I want to \input"|..."
to invoke a shell command and consume its output as LaTeX code.
In particular, I want to write something like:
\expandableShellCmdProtectCmdArgsWithBackslashes{echo}{2(3)}
and have it call the shell with:
echo \2\(\3\)
escaping all the "arg" characters with backslashes.
The reason is to escape shell-special characters like (
.
The desired result is that echo
prints 2(3)
and that is consumed as LaTeX input.
The attempt below via \str_map_inline:nn
+ \c_backslash_str
fails, producing strange slanted double-quote symbols in place of all of the locations where I'd expect backslashes before invoking the shell, and the actual shell command executed doesn't include most of the desired input.
How can I prepend a backslash before each character of an \input
command?
\documentclass[11pt]{article}
\begin{document}
\makeatletter
\newcommand{\expandableShellCmd}[1]{ \@@input"|#1"} % \input is not expandable
\makeatother
\ExplSyntaxOn
\NewDocumentCommand{\protectWithBackslashes}{m} { % prepends every character of the input with a backslash
\str_map_inline:nn{#1}{\c_backslash_str##1}
}
\NewDocumentCommand{\expandableShellCmdProtectCmdArgsWithBackslashes}{mm} {% shell command, args
% Pass the input string as a cmd to the shell, but prepend each char in the args with a backslash
\exp_args:Ne\expandableShellCmd{#1~\protectWithBackslashes{#2}} % Fully expand the argument before invoking the shell
}
\ExplSyntaxOff
\expandableShellCmdProtectCmdArgsWithBackslashes{echo}{2(3)} % want to run in shell "echo \2\(\3\)" which produces output "2(3)"
% However, this produces PDF output:
% “2“(“3“)”
% and the console output shows that the shell command executed was (without quotes) "|echo "
\end{document}
\
quote every character? it's usually easier to'
delimit the entire string, then just use\'
for'
OT1
fonts have open quotes in the\
slot you would need\texttt
or better add\usepackage[T1]{fontenc}
but then you get this\
before every character is conceptually simpler than'
at the ends + special protection for'
in the middle.