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:
- I am using a JSON file as input in the Python module that handles all the data ETL and context rendering
- 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.
- 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.
- I also have a
header.txtfile for the front page where I got entries such as `title: "This is a report about {{title}}" and date: "{{date}}" - I have also tried to add
\graphicspath{{path/to/images}}in my main.texfile but for some reason this gets ignored. - I have also tried to add to the
.texfile 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!
from jinja2 import Templateor something similar. – Marijn Apr 29 '21 at 16:39