0

I am trying to use a context variable that is rendered at runtime within the LaTeX/MarkDown code block that generates a figure. In principle, I would like to allow for dynamic paths to the images to include in the figure.

This is a working piece of code:

\begin{figure}
  \centering
     \IfFileExists{path/to/my_image.png}
     {\includegraphics[width=0.9\textwidth]{path/to/my_image.png}
     {\includegraphics[width=0.2\textwidth]{path/to/image_not_available.png}
     \caption{My caption.}
     \label{Figure.1}
\end{figure}

Now, I have a context variable which I set at runtime:

Path to the image folders: {{images_folder}}

This works in "plain" MarkDown, but I cannot find a way to use this variable within the figure code block. I tried (with no success):

 {\includegraphics[width=0.9\textwidth]{{{images_folder}}/my_image.png}

or

{\includegraphics[width=0.9\textwidth]{'{{images_folder}}'/my_image.png}

Do I need to somehow escape the {{ }} construct or? Can you please give some advice? Thank you

EDIT: to give a bit more insight as @Marijn requested wrt the sequence of steps:

  1. I am using a JSON file as input in the Python module that handles all the data ETL and context rendering
  2. Once the data is parsed and loaded into a dictionary, a RenderContext object is initialized with all the metadata and data to populate all the context variables used throughout the report that is generated.
  3. These variables are then fed from the RenderContext dictionary into the MarkDown templates, based on their specific key (i.e., the RenderContext <key,value> pair "number": 10 becomes {{number}} in the MarkDown plain text.
  4. I also have a header.txt file for the front page where I got entries such as `title: "This is a report about {{title}}" and date: "{{date}}"
  5. I have also tried to add \graphicspath{{path/to/images}} in my main .tex file but for some reason this gets ignored.
  6. I have also tried to add to the .tex file the environment variable that hosts this path (e.g., IMAGES="/absolute/path/to/images/" following this post but with no success. Does not want to recognize the newly defined command.

As mentioned, in plain MarkDown text, I can access the variables just fine, but when it comes to figure or table blocks, I cannot make it retrieve the variable value. I have also tried with \includegraphics[width=.....]{$images_folder$} as i saw it implemented in the main .tex template, tried to escape it with \ etc.. etc.. but no success.

An example of a plain MarkDown text using one Jinja context variable would be:

This sample was found to have a contamination of {{contamination or '\\notset'}}.

where if this key,value pair for contamination is not found in RenderContext, I would display a custom [not_set] text in red to make me aware that something goes wrong.

Thanks again for the help!

Marius
  • 101
  • Welcome to TeX.SE. When and how exactly does Jinja process this block? Could you add the sequence of steps you use to generate the end result to your question? Does variable substitution work in other parts of the LaTeX code? Maybe the triple brackets are confusing Jinja? – Marijn Apr 29 '21 at 13:15
  • Thanks @Marijn! I've updated my question to give a bit more insight. Let me know if that suffices or if more details are needed. – Marius Apr 29 '21 at 16:08
  • Thanks for the update, it clarifies the process somewhat. However, since this is a fairly non-standard setup (standard would be running LaTeX on a pure LaTeX file, or producing LaTeX from RMarkdown for example) I think it would help if you explain step by step how your issue can be reproduced. So: given this input file(s), run this command, then that command, then you get the error. If that procedure involves setting up a server, then try to simplify by writing a simulation Python script based on from jinja2 import Template or something similar. – Marijn Apr 29 '21 at 16:39
  • Hi Marijn and thanks for the comment. I'm afraid that providing you with input files is not that easy as I am dealing with sensitive data. Also, it would take me way too long to develop a simplified reproducibility scenario for all this stack for what this question boils down to: how to escape a Jinja context variable in a LaTex/MarkDown figure or table block. That is it. I do believe that the amount of information provided in the question is enough. I can assure you that the variables are populated as I can print them in normal, plain text, MarkDown/LaTex. – Marius Apr 30 '21 at 07:24
  • 1
    It's understandable that you don't want to share sensitive data, but maybe it is possible to make an example with made-up dummy data? The information you provided in your question may be sufficient for somebody to answer your question, however I fear that without a directly reproducible example the question will not get much attention and will remain unanswered. I'm happy to be proven wrong of course :) – Marijn Apr 30 '21 at 10:40

0 Answers0