1

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?

user202729
  • 7,143
HappyFace
  • 153
  • 5
  • You might want to escape characters that have a special meaning in TeX, that is { } $ _ ^ # & \ ~ and %. Also see: https://tex.stackexchange.com/q/34580/47927 – Jasper Habicht Dec 20 '23 at 17:21
  • Are you inputting VAR_VALUE somewhere 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
  • You could write \newcommand{\varname}{\texttt{\detokenize{$VAR_VALUE}}} for the value, but you can't really have $VAR_NAME as macro name (well, you could, but you shouldn't). – campa Dec 20 '23 at 17:41
  • @JasperHabicht Indeed, this is what I was doing. But using \_ 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
  • @campa I thought \texttt is 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:05
  • @cfr VAL_VALUE is manually inputted by myself, but not in relation to LaTeX. E.g., I have export 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
  • 1
    @HappyFace If you plan to use the image, as opposed to typesetting the path, you probably don't need or want to escape it. I don't do anything special when processing paths and I have quite a lot of code involving this kind of configuration. If you escape things, I'm not sure the result will work as you expect and you certainly don't need to escape things for it to work in e.g. \includegraphics or \graphicspath or \includepdf etc. – cfr Dec 20 '23 at 18:15
  • 3
    If your build system replaces the placeholders correctly, then what needs to be done really depends on what you are doing with the variable once it is defined. – cfr Dec 20 '23 at 18:18
  • \detokenize is your friend here. – David Carlisle Dec 20 '23 at 18:28
  • @cfr Thanks. So my current understanding is that special chars like _ 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 \detokenize useful here?! – HappyFace Dec 20 '23 at 20:37
  • @JoséCarlosSantos No, that question is about (manually) typesetting some arbitrary text. I want to set some variables with an arbitrary string, not typeset anything. I also need the solution to be automated in some programming language. – HappyFace Dec 20 '23 at 20:44
  • Do you really have {} 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 \includegraphics or \file_input:n or 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
  • LaTeX can do different things. E.g., It can do typesetting whereby characters of category 11 and 12 of the .tex-input-file are directives for choosing a glyph of a font or to construct a symbol from several glyphes. E.g., it can write things to screen or external text file. E.g., wwhen processing a command \input or \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
  • What you can do is having TeX read the characters in question in verbatim-catcode-régime and then change the catcode-régime appropriately for the task in question where the characters are to be processed and then apply \scantokens to the characters previously read in verbatim catcode-régime. – Ulrich Diez Dec 20 '23 at 22:21
  • @HappyFace your question is still very unclear what you mean by escape or in what context. for example _, {} , $ 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
  • 1
    If things are needed only in the context of file names and file paths, then with primitives like \read, \openin, \openout, \input (which is renamrd to \@@input in 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
  • 1
    With macros like \includegraphics characters 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 \detokenize or 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
  • 2
  • 1
    The practical solution is to use the external program to copy/create a symbolic link of your desired file to something with nice name, then include that instead. The non-practical solution is to delve deep into the source code of TeX and LaTeX to see how exactly things are implemented and which special characters break, and how to workaround it. (to the best of my knowledge, high level commands like \includegraphics officially does not support special character in file name and tell the user to rename the files. – user202729 Dec 21 '23 at 08:42
  • The official documentation is in e.g. the LaTeX companion, which is a textbook which you need to buy.) – user202729 Dec 21 '23 at 08:47
  • @user202729 Thanks, you can submit your comment as an answer. – HappyFace Dec 23 '23 at 08:52
  • 1
    @UlrichDiez Thanks. I indeed only want \includegraphics and \input to 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}} where A is my path that has been processed to have escaped {} with \{ and \} work? – HappyFace Dec 23 '23 at 08:55

0 Answers0