[LyX/master] Fix backslash LaTeXifying in InsetCommand

Juergen Spitzmueller spitz at lyx.org
Mon Apr 1 09:00:04 UTC 2024


commit a020bbc4a8143cff34e80442caf71d3b2ff8ccd3
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Mon Apr 1 10:57:27 2024 +0200

    Fix backslash LaTeXifying in InsetCommand
    
    \ 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 | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp
index 94b9a2c8a2..034f9d59c8 100644
--- a/src/insets/InsetCommandParams.cpp
+++ b/src/insets/InsetCommandParams.cpp
@@ -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);


More information about the lyx-cvs mailing list