3

I want to know how can I write a lopp in order to put some graphs on my document with a different caption. I have n graphs with a variable name, i.e. "activos_t" with the label "Activos Totales" & "pasivos_t" with the label "Pasivos Totales". I want the caption to be different for each variable name contained in the following loop

\foreach \w in {activos_t,pasivos_t}{

\begin{figure}[H]
\centering 
\caption{"label"}
\label{fig:Figura }
\includegraphics[height=6cm]{C:/Users/G15185/Desktop/ProyectoLATEXentregable/\w.png}
\end{figure}
}

Does anybody know how can I use an "if else" statement or any other condition to achieve that?

1
1

You can use \w in the \caption. However, because it contains an underscore, you can't typeset it as is outside of math mode. So instead, I \detokenize it.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\begin{document}
\foreach \w in {activos_t,pasivos_t}{
\begin{figure}[ht]
\centering 
\caption{\detokenize\expandafter{\w} Totales}
\label{fig:Figura }
\includegraphics[height=6cm]{C:/Users/G15185/Desktop/ProyectoLATEXentregable/\w.png}
\end{figure}
}
\end{document}

enter image description here

To get the \w values capitalized in the \caption, one must expand and save the \detokenize result and pass it to \titlecap.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tikz,titlecaps}
\begin{document}
\foreach \w in {activos_t,pasivos_t}{
\begin{figure}[ht]
\centering
\xdef\tmp{\detokenize\expandafter{\w} totales}
\expandafter\caption\expandafter{\expandafter\titlecap\expandafter{\tmp}}
\label{fig:Figura }
\includegraphics[height=6cm]{C:/Users/G15185/Desktop/ProyectoLATEXentregable/\w.png}
\end{figure}
}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.