Consider known latex text macros (basically the logos) in convertaTeXCommands()

This commit is contained in:
Juergen Spitzmueller 2024-08-16 11:23:57 +02:00
parent e1b6ad7980
commit 712600dd6c
3 changed files with 42 additions and 4 deletions

View File

@ -614,23 +614,30 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
}
docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_xhtml)
/// text macros we can convert beyond unicodesymbols
char const * const known_text_macros[] = {"LyX", "TeX", "LaTeXe", "LaTeX", ""};
char const * const known_text_macros_out[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", ""};
docstring Encodings::convertLaTeXCommands(docstring const & str, bool const literal_math)
{
docstring val = str;
docstring ret;
docstring mret;
docstring cret;
bool scanning_cmd = false;
bool scanning_math = false;
bool is_section = false;
bool escaped = false; // used to catch \$, etc.
bool skip_space = false;
while (!val.empty()) {
char_type const ch = val[0];
// if we're scanning math, we collect everything until we
// find an unescaped $, and then try to convert this piecewise.
if (scanning_math) {
if (for_xhtml) {
if (literal_math) {
// with xhtml, we output everything until we
// find an unescaped $, at which point we break out.
if (escaped)
@ -688,6 +695,7 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
continue;
}
if (isAlphaASCII(ch)) {
cret += ch;
is_section = false;
val = val.substr(1);
escaped = false;
@ -703,6 +711,17 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
scanning_cmd = false;
}
// check if it's a know text macro
// If so, output and skip the following space
if (!cret.empty()) {
int const n = findToken(known_text_macros, to_ascii(cret));
if (n != -1) {
ret += known_text_macros_out[n];
skip_space = true;
}
cret.clear();
}
// was the last character a \? If so, then this is something like:
// \\ or \$, so we'll just output it. That's probably not always right...
if (escaped) {
@ -728,6 +747,13 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
continue;
}
if (isSpace(ch) && skip_space) {
val = val.substr(1);
skip_space = false;
continue;
}
skip_space = false;
// Change text mode accents in the form
// {\v a} to \v{a} (see #9340).
// FIXME: This is a sort of mini-tex2lyx.
@ -780,6 +806,14 @@ docstring Encodings::convertLaTeXCommands(docstring const & str, bool const for_
escaped = true;
val = val.substr(1);
}
// check if it's a know text macro
// If so, output and skip the following space
if (!cret.empty()) {
int const n = findToken(known_text_macros, to_ascii(cret));
if (n != -1)
ret += known_text_macros_out[n];
}
return ret;
}

View File

@ -352,7 +352,7 @@ public:
/// converts a string containing LaTeX commands into unicode
/// for display.
static docstring convertLaTeXCommands(docstring const & str,
bool const for_xhtml = false);
bool const literal_math = false);
///
enum LatexCmd {
///

View File

@ -266,7 +266,11 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons
// We do this on all levels.
// We don't do it if the level already contains a '@', though.
// We use a somewhat "plain" representation for this
docstring const spart = Encodings::convertLaTeXCommands(thislevel);
docstring spart = Encodings::convertLaTeXCommands(thislevel);
// if convertLaTeXCommands() returns nothing, we fall back
// to the command name without backslash
if (trim(spart).empty())
spart = ltrim(thislevel, "\\");
processLatexSorting(os, runparams, thislevel, spart, escape_char);
}
} else {