I have a build system for my latex files that injects some environment variables into the latex file that is to be compiled.
Usually, the environment variables contain path to image files. So, a build system may generate a TeX file that looks like:
\newcommand{\myfilepath}{/folder/file.png}
And later on, it may be used such as:
\includegraphics{\myfilepath}
But, this breaks if the file path /folder/file.png contains special characters.
How can I write the TeX code generation program in a way that makes the \includegraphics line works?
{}$_^#&\~and%. Also see: https://tex.stackexchange.com/q/34580/47927 – Jasper Habicht Dec 20 '23 at 17:21VAR_VALUEsomewhere or is it being automatically generated? In the former case, put the escapes in when setting it. In the latter case, it probably depends on the details of how exactly it's generated. – cfr Dec 20 '23 at 17:28\newcommand{\varname}{\texttt{\detokenize{$VAR_VALUE}}}for the value, but you can't really have$VAR_NAMEas macro name (well, you could, but you shouldn't). – campa Dec 20 '23 at 17:41\_instead of_actually broke the build. It seems that_does NOT need to be escaped in\newcommand{name}{...}. Can someone shed any info on this? – HappyFace Dec 20 '23 at 18:03\textttis for formatting a text as monospace? I just want to escape the text.VAR_NAMEs generally are well-behaved and do not need escaping. – HappyFace Dec 20 '23 at 18:05VAL_VALUEis manually inputted by myself, but not in relation to LaTeX. E.g., I haveexport DEP_LOGO=~/../logo.png. I could manually escape all these, but I am looking for an automated solution. An automated solution will allow me to incrementally automate more and more, so it's worth it even if it is overkill for my current needs. – HappyFace Dec 20 '23 at 18:08\includegraphicsor\graphicspathor\includepdfetc. – cfr Dec 20 '23 at 18:15\detokenizeis your friend here. – David Carlisle Dec 20 '23 at 18:28_behave differently when they are the argument of\includegraphics. Is there a documentation page that explains this behavior? Some characters will need to be escaped even in that context though, e.g.,{}? Is\detokenizeuseful here?! – HappyFace Dec 20 '23 at 20:37{}in the names of files and directories? In general, you should avoid using special characters in file and directory names irrespective of the current question. An underscore would normally need escaping, but I don't do anything special when it's part of a directory name I'm passing to\includegraphicsor\file_input:nor whatever. Note that it really isn't clear what you are trying to do or what you mean by 'automated in some programming language'. Indeed, that doesn't sound like a question for this site .... – cfr Dec 20 '23 at 21:13\inputor\includegraphics, it can take sequences of characters for filenames. Thus the kind of escaping depends on what is to be done wih the characters in question. – Ulrich Diez Dec 20 '23 at 22:21\scantokensto the characters previously read in verbatim catcode-régime. – Ulrich Diez Dec 20 '23 at 22:21_,{},$need no special treatment in\includegraphics, but~would need\string~so\includegraphics{\string~/aaa_b#.png}should work fine as would\includegraphics{\detokenize{~/aaa_b#.png}}with the latter being easier for automated situations as you can always apply it to the whole string. – David Carlisle Dec 20 '23 at 23:46\read,\openin,\openout,\input(which is renamrd to\@@inputin LaTeX) afaik only characters that usually have one of the category codes 0(escape) - backslash, 7(superscript) -^, 9(ignored), 10(space) - space and horizontal tab, 13(active) -~, 14(comment) -%or 15(ignored) require special attention. In case of processing file names/file paths inside tables, characters of category 4(alignment tab) -&, require special attention as well. – Ulrich Diez Dec 21 '23 at 01:16\includegraphicscharacters of category 1(begin group) -{and characters of category 2(end group) -}need special treatment as well so that they can make it into an undelimited macro argument. If using\detokenizeor the like, characters of category 6(parameter) -#- also need special treatment for avoiding them to be doubled. – Ulrich Diez Dec 21 '23 at 01:17\includegraphicsofficially does not support special character in file name and tell the user to rename the files. – user202729 Dec 21 '23 at 08:42\includegraphicsand\inputto work with these. You can turn your comments into an answer. I am still a bit confused about how each of the special characters you have enumerated should be escaped. Does\includegraphics{\detokenize{A}}whereAis my path that has been processed to have escaped{}with\{and\}work? – HappyFace Dec 23 '23 at 08:55