Giter Club home page Giter Club logo

Comments (3)

ppizarror avatar ppizarror commented on August 19, 2024 1

Implemented in #35.

from pydetex.

ppizarror avatar ppizarror commented on August 19, 2024

The example "I am \MakeUppercase{Happy}" => "I am" should be translated to "I am \MakeUppercase{Happy}" => "I am HAPPY". Thus, we should add the following:

\uppercase{text}
\lowercase{text}
\MakeUppercase{text}
\MakeLowercase{text}

To add these methods, the function

PyDetex/pydetex/parsers.py

Lines 702 to 787 in 3b39b08

def output_text_for_some_commands(
s: str,
lang: str
) -> str:
"""
Replaces the command for a particular text.
:param s: Latex string code
:param lang: Language tag of the code
:return: Text string or empty if error
"""
# Stores the commands to be transformed
# (
# command name,
# [(argument number, argument is optional), ...],
# tag to be replaced,
# total commands,
# font_tag ('tex_text_tag' if None),
# font_content ('tex_text_tag_content' if None),
# add new line (before, after)
# )
# The font format is like .... [font tag]YOUR TAG {[font content]YOUR CONTENT} ...[font normal]. In that case, tag to be
# relaced is 'YOUR TAG {0}, {1}
# All *arguments will be formatted using the tag
commands: List[Tuple[str, List[Tuple[int, bool]], str, int, Optional[str], Optional[str], Tuple[bool, bool]]] = [
('caption', [(1, False)], LANG_TT_TAGS.get(lang, 'caption'), 1, None, None, (False, True)),
('chapter', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('chapter*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('em', [(1, False)], '{0}', 1, 'normal', 'bold', (False, False)),
('emph', [(1, False)], '{0}', 1, 'normal', 'italic', (False, False)),
('href', [(2, False)], LANG_TT_TAGS.get(lang, 'link'), 2, None, None, (False, False)),
('insertimage', [(3, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 3, None, None, (False, True)),
('insertimage', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, False)),
('insertimageboxed', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, True)),
('insertimageboxed', [(5, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 5, None, None, (False, True)),
('paragraph', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('section', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('section*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subfloat', [(1, True)], LANG_TT_TAGS.get(lang, 'sub_figure_title'), 1, None, None, (False, True)),
('subparagraph', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsection', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsection*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsubsection', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsubsection*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsubsubsection', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subsubsubsection*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('textbf', [(1, False)], '{0}', 1, 'normal', 'bold', (False, False)),
('textit', [(1, False)], '{0}', 1, 'normal', 'italic', (False, False)),
('texttt', [(1, False)], '{0}', 1, 'normal', 'normal', (False, False))
]
new_s = ''
# Get the commands
cmd_args = ut.get_tex_commands_args(s)
for c in cmd_args:
for cmd in commands:
if c[0] == cmd[0]:
_, cmd_args, cmd_tag, total_commands, font_tag, font_content, cmd_newline = cmd
if font_tag is None:
font_tag = 'tex_text_tag'
if font_content is None:
font_content = 'tex_text_tag_content'
if len(c) - 1 == total_commands:
args = []
for j in cmd_args:
cmd_argnum, cmd_is_optional = j
if len(c) - 1 >= j[0] >= 0 and c[cmd_argnum][1] == cmd_is_optional:
argv = c[cmd_argnum][0].replace('\n', ' ') # Command's argument to process
argv = remove_commands_param(argv, lang) # Remove commands within the argument
argv = argv.strip()
if argv != '':
args.append(argv)
if len(args) == len(cmd_args):
# Add format text
for a in range(len(args)):
args[a] = FONT_FORMAT_SETTINGS[font_content] + args[a] + FONT_FORMAT_SETTINGS[font_tag]
text = cmd_tag.format(*args)
text = FONT_FORMAT_SETTINGS[font_tag] + text + FONT_FORMAT_SETTINGS['normal']
if cmd_newline[0]:
text = _TAG_NEW_LINE + text
new_s += text
if cmd_newline[1]:
new_s += _TAG_NEW_LINE
break
return new_s.strip()
must be edited. I was thinking of allowing functions/lambdas in commands:

commands = [...,
('MakeUppercase', [(1, False)], lambda s: s.upper(), 1, 'normal', 'normal', (False, False))
]

In that case, line

PyDetex/pydetex/parsers.py

Lines 778 to 779 in 3b39b08

text = cmd_tag.format(*args)
text = FONT_FORMAT_SETTINGS[font_tag] + text + FONT_FORMAT_SETTINGS['normal']
should support methods instead of calling string.format, for example:

if callable(cmd_tag):
    text = cmd_tag(*args)
else:
    text = cmd_tag.format(*args)

With such methods, I think we could easily add more in the future. This solution differs from #28 because of the nature of the input. Citeauthor requires cites and a different font type; in this case, it only takes one arg of "normal" text.

Let me know if you can add these methods =). Greetings

from pydetex.

xionghuichen avatar xionghuichen commented on August 19, 2024

Thanks for the response. I have not tried the solution yet, but it looks reasonable. I will reply to you as soon as possible after I tried it. :)

from pydetex.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.