[pstricks] General question about loops

Toche, Patrick ptoche at cityu.edu.hk
Sun Nov 20 18:01:29 CET 2011


Dear Herbert,

Thanks for your help, once again, I'm sorry if I wasn't clear about the problems. I hope it'll be clearer this time around. The good news is that I have found some workaround. I hope the description of the problems I ran into can be of some help to someone some day...

Let me summarize the problem (and attempted solutions) so far.

#1. Writing a loop to include images with names like Name1.ps, Name2.ps, yes I can do that (see my original post). Below are two examples, one based on the pgffor package, the other based on multido. Both work with the article and beamer class. Both work with latex/ps2pdf and pdflatex.

using pgffor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}%
\usepackage{graphicx}%
\usepackage{pgffor}%
\begin{document}%

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

\end{document}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

using multido
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{beamer}%	
\usepackage{graphicx}%
\usepackage{multido}%
\begin{document}%

\multido{\n=1+1}{2}{%
  \begin{frame}[c]%
    \begin{center}%
      \includegraphics[width=\textwidth]{\n.ps}%
    \end{center}%
  \end{frame}%
}%

\end{document}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#2. However, as I explained in my original post, the file names I have are not numbered, they are arranged in a list.

My first attempt did not appear to work, for some reason that remains a little unclear. Let me describe a slightly revised version here: I have removed the file extensions and let the compilation guess what they are. It seems to work better.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


You will be pleased to hear that the above works flawlessly with postscript figures when compiled with latex and ps2pdf. It can be made to work with pdflatex too, with figure extensions like png or pdf, but in a funny sort of way: by adding an extra comma in the file list and letting pdflatex expect an extra figure, which produces an error message but works. The extra comma is not needed when compiling with latex.

(I don't suppose you would be thrilled to hear that an error message is produced, however this is a workaround for use with pdflatex only!)

The reason why it was not working in its first version has to do with the file extensions not being properly appended to the name, a problem that was NOT fixed by adding inverted commas, as in "Name.ps", not even with the grffile package. Thanks for these suggestions Herbert, I guess they should have helped, not sure why they didn't. Removing the file extensions altogether saved the day here.

Though I have included it in the code above, I didn't find that \DeclareGraphicsExtensions helped. My system knew what the file extensions were, and adapted to latex/ps2pdf or pdflatex without being told to. 

Now, let me summarize my attempts based on the pstricks package, following several of your suggestions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}%
%\usepackage{grffile}%
\usepackage{graphicx}%
\usepackage{pstricks}%

\newcommand*\List{%
   Name1.ps,
   Name2.ps
}%

\newcommand\insertfigure[1]{%
%  \includegraphics[width=\textwidth]{"#1"}%
  \includegraphics[width=\textwidth]{#1}%
}%

\begin{document}%

\psforeach{\name}{\List}{\insertfigure{\name}\endgraf}%

\end{document}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The code above, compiled with latex/ps2pdf inserts only the first figure, not the second figure. Why? I do not know. While the output is incomplete here too, it is obviously a different problem from the one encountered above when compiling with pdflatex, because inserting an extra comma in the list does not induce latex to include the second figure. This time around, omitting file extensions is counterproductive, as it doesn't work any better. Including or not the package grffile does not help (and that is strange, because it's supposed to help). Omitting the inverted commas does help (compilation aborted otherwise).

#3. And your latest suggestion,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
\usepackage{graphicx,pstricks,filecontents}

\begin{filecontents*}{foo1.ps}
%!ps
%%BoundingBox: 0 0 100 100
0 0 moveto 100 100 lineto
1 setlinewidth 1 0 0 setrgbcolor stroke
\end{filecontents*}
\begin{filecontents*}{foo2.ps}
%!ps
%%BoundingBox: 0 0 100 200
0 0 moveto 100 200 lineto
1 setlinewidth 0 0 1 setrgbcolor stroke
\end{filecontents*}

\begin{document}

\psforeach{\A}{1,2}{\includegraphics{foo\A}}

\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

This compiles properly here too, it produces one image with 2 lines, one blue and one red. While I do not understand the postscript code, I gather that foo1.ps and foo2.ps are two images and that the \psforeach code inserts them. Am I correct in saying that they are printed on top of each other? Can the \psforeach command be used to insert an external file?

These are purely academic questions as my problem has now been solved (with an error message in the case of pdflatex, without any glitch in the case of latex/ps2pdf, using the pgffor package, see above).

Many thanks Herbert for your patience and generosity.



More information about the PSTricks mailing list