]> git.lyx.org Git - features.git/commitdiff
Fix backslash LaTeXifying in InsetCommand
authorJuergen Spitzmueller <spitz@lyx.org>
Mon, 1 Apr 2024 08:57:27 +0000 (10:57 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Mon, 1 Apr 2024 08:57:27 +0000 (10:57 +0200)
\ was transformed very early to \textbackslash{}, but then the following
routines escaped braces in the string, so we wrongly ended up in
\textbackslash\{\} and "\{} in the output

src/insets/InsetCommandParams.cpp

index 94b9a2c8a2e7897be9fce2d393e36be3ea1db3b0..034f9d59c8700ed885848ce46a9d7a0bdd21d3d6 100644 (file)
@@ -455,7 +455,21 @@ docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
        // LATEXIFY, ESCAPE and NONE are mutually exclusive
        if (handling & ParamInfo::HANDLING_LATEXIFY) {
                // First handle backslash
-               result = subst(command, from_ascii("\\"), from_ascii("\\textbackslash{}"));
+               // we cannot replace yet with \textbackslash{}
+               // as the braces would be erroneously escaped
+               // in the following routines ("\textbackslash\{\}").
+               // So create a unique placeholder which is replaced
+               // in the end.
+               docstring bs = from_ascii("@LyXBackslash@");
+               // We are super-careful and assure the placeholder
+               // does not exist in the string
+               for (int i = 0; ; ++i) {
+                       if (!contains(command, bs)) {
+                               result = subst(command, from_ascii("\\"), bs);
+                               break;
+                       }
+                       bs = from_ascii("@LyXBackslash") + i + '@';
+               }
                // Then get LaTeX macros
                pair<docstring, docstring> command_latexed =
                        runparams.encoding->latexString(result, runparams.dryrun);
@@ -493,6 +507,8 @@ docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
                                                        result.replace(pos, 1, backslash + chars_escape[k] + term);
                                }
                }
+               // set in real backslash now
+               result = subst(result, bs, from_ascii("\\textbackslash{}"));
        }
        else if (handling & ParamInfo::HANDLING_ESCAPE)
                result = escape(command);