Graphics, Figures & TablesLoop over Figures

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Loop over Figures

Post by ptoche »

My ultimate objective is to pdflatex a beamer document in which each slide displays a figure from the current directory, where the names and number of figures is not a priori known. (explanation: the figures are those that do not make it to the final presentation, but might prove useful to have at hand just in case).

Thus I want to loop over a list of the figures. I have a batch file that automatically list all the files in the current folder, so I can \include or \input that into my document. In other words, I do have the list of names of the figures to be included.

The following works if all my figures are named Name1.pdf, Name2.pdf, etc.

Code: Select all

\documentclass{beamer}%
\usepackage{graphicx}%
\usepackage{pgffor}%

\begin{document}%

\foreach \index in {1, ..., 2} 
{%
  \includegraphics[width=\textwidth]{Name\index.pdf}\par%
}%

\end{document}
But my figures will not have such nice names. And some will be pdf and some will be png. Edit: latex does not accept png, if I understand correctly, but pdflatex certainly does.

The following is my best effort, but it doesn't work.

Code: Select all

\documentclass{beamer}%
\usepackage{graphicx}%
\usepackage{pgffor}%

\newcommand*{\List}%
{%
Name1.pdf,
Name2.png
}%

\begin{document}%

\foreach \name in \List%
{%
  \begin{frame}%
    \begin{figure}%
      \begin{minipage}[b]{\textwidth}%
      \centering%
       \includegraphics[width=\textwidth]{\name}%
      \end{minipage}%
    \end{figure}%
  \end{frame}%  
}%

\end{document}
I also tried {"\name"} instead of {\name}. The code inserts the first figure in the loop (no matter its extension, pdf or png) but not the second figure in the loop.

Any suggestions?

many thanks.

EDIT: Simplified my question, hopefully it is clearer.
Last edited by ptoche on Thu Nov 10, 2011 1:01 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Loop over Figures

Post by ptoche »

I'm still looking for a solution. Any suggestions will be greatly appreciated. Pointer to a package, anything.

Can multido be tweaked to take a list of names rather than indices?

Along the lines of:

Code: Select all

\documentclass{beamer}%	
\usepackage{graphicx}%
\usepackage{multido}%

\newcommand*{\List}{%
Name1.png,
Name2.pdf
}%

\newcommand{\beamerfigure}[1]{%
  \begin{frame}[c]%
    \begin{center}%
      \includegraphics[width=\textwidth]{"Name#1"}%
    \end{center}%
  \end{frame}%
}%

\begin{document}%

\beamerfigure{1}%works
\beamerfigure{2}%works

\multido{\name=\List}{2}{\beamerfigure{\name}}%can multido be tweaked to take a list of names rather than indices?

\end{document}
Compiling spits out a series of errors, naturally:
! Undefined control sequence.
->\errmessage LaTeX Error: File `"Name\name
"' not found.

See the LaTeX m...
l.25 ...tido{\name=\List}{2}{\beamerfigure{\name}}
%can multido be tweaked to...
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Loop over Figures

Post by ptoche »

So far no-one has shown interest in my question, perhaps it is misplaced. At any rate, let me describe a solution I have concocted based on suggestions here:

http://www.tug.org/pipermail/pstricks/2011/009357.html

Feel free to comment and/or offer a fix for the pdflatex glitch.

Code: Select all

\documentclass{article}%
\usepackage{graphicx}%
\usepackage{pgffor}%

\usepackage{ifpdf}% supposedly helps recognizing file extensions
  \ifpdf%
    \DeclareGraphicsExtensions{.pdf,.png,.jpg}%
  \else%
    \DeclareGraphicsExtensions{.eps,.ps}%
  \fi%


\newcommand*{\List}{% % file extensions removed
FunnyName,
OddName,
}% 
% extra comma after the last name "needed" for pdflatex
% produces error message but output correct
% extra comma not needed with latex/ps2pdf


\begin{document}%

\foreach \name in \List {%
  \includegraphics[width=\textwidth]{\name}\par%
}%

\end{document}

And here a version for beamer:

Code: Select all

\documentclass{beamer}%
\usepackage{graphicx}%
\usepackage{pgffor}%

\usepackage{ifpdf}%
  \ifpdf%
    \DeclareGraphicsExtensions{.pdf,.png,.jpg}%
  \else%
    \DeclareGraphicsExtensions{.eps,.ps}%
  \fi%

\newcommand*{\List}{%
FunnyName,
OddName,
}%

\newcommand{\beamerfigure}[1]{%
  \begin{frame}[c]%
    \begin{center}%
      \includegraphics[width=\textwidth]{#1}%
    \end{center}%
  \end{frame}%
}%

\begin{document}%

\foreach \name in \List {%
  \beamerfigure{\name}%
}%

\end{document}%
Post Reply