yannis assael | the blog

  • Home
  • About
  • Categories
    • Android
    • Computing
    • iOS
    • Machine Learning
    • MacOSX
  • GitHub
  • Mobile Apps
  • yannisassael.com


Latex foreach multiple figures loop

Written by iassael on 28/11/2014. Posted in computing, general

800px-LaTeX_logo.svgUsing Latex, many times you want to print multiple figures that have similar names like figure_1.pdf, figure_2.pdf etc…

Instead of hardcoding everything, the foreach package saves lives!

More specifically, you can write something like this:

\begin{figure}[H]
\centering
\foreach \n in {1,3,5,10,20}{
\begin{subfigure}{.18\linewidth}
\centering
\includegraphics[width=\textwidth]{figs/question_9_\n}
\subcaption{\n Trees}
\end{subfigure}
}
\caption{RF various number of trees evaluation.}
\end{figure}

Tags: efficiently, figures, for, foreach, latex, loop, multiple, print, subfigures

Trackback from your site.

Comments (1)

  • Avatar

    cla4speed

    30/01/2019 at 3:13 pm | #

    Your article is well explained and objective! Thanks.
    But the name of the package is not “foreach”. It took me some time to find out why my code was crashing. But because the package needed is calle “pgffor”. Information I got from here: https://texblog.org/2015/10/09/automated-sub-figure-generation-using-a-loop-in-latex/
    I thought maybe you would like to know…

Leave a comment