[Tex/LaTex] Inserting multiple figures using loop and python in Windows 7 [Solved]

pythonpythontexwindows

Hello because I have a set of figures in a folder that I want to put in a document sequentially, I am trying to create an automatic procedure.

Looking around I found this old post Inserting figures using loops

I really like the idea to use python but I have some problems with the package python.sty or pythontex.sty, I don't really understand how they work.

My code is:

\documentclass{article}

\usepackage{python}
\usepackage{graphicx}

\begin{document}

\begin{python}
import os
directory = "."
extension = ".png"
files = [file for file in os.listdir(directory) if 
file.lower().endswith(extension)]

for file in files:
   print(r"\begin{figure}[!ht]")
   print(r"\centering")
   print(r"\includegraphics[width=10cm,height=10cm]{%s}" % file)
   print(r"\caption{File %s}" % file)
   print(r"\label{Serie}")
   print(r"\end{figure}")
\end{python}

\end{document}

it's exactly the same in the other post, but when I run it with the command:

pdflatex.exe –enable-write18 .\test.tex

I obtain the error:

pdflatex.exe : 'cat' is not recognized as an internal or external command,
At line:1 char:1
+ pdflatex.exe –enable-write18 .\test.tex
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ('cat' is not re…ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

operable program or batch file.
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\etexcmds.sty"))))
! I can't find file `test.py.out'.

\def
l.21 \end{python}

And when I use pythontex.sty I don't understand the steps I have to follow.
I modify the code to use this other package, run with the same command and the output is:

No file pythontex-files-test/test.pytxmcr.
Run PythonTeX to create it.
("C:\Program Files\MiKTeX 2.9\tex\context\base\supp-pdf.mkii"
[Loading MPS to PDF converter (version 2006.09.02).]
) ("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\pdftexcmds.sty"
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\ifpdf.sty"))
("C:\Program Files\MiKTeX 2.9\tex\latex\oberdiek\epstopdf-base.sty"
("C:\Program Files\MiKTeX 2.9\tex\latex\oberdiek\grfext.sty"
("C:\Program Files\MiKTeX 2.9\tex\generic\oberdiek\kvdefinekeys.sty")))
(test.aux) )
No pages of output.
Transcript written on test.log.

Could anyone give me some advice? The OS I am using is Windows 7.

Thanks a lot!

Ciccio

Best Answer

All of them should work. The code you posted works perfectly for the python package.

Here I don't have a solution to the error you are getting, but perhaps a solution to your image-including problem.

Here are two other approaches:

Pythontex

With pthontex you just have to replace:

  1. \usepackage{python} by \usepackage{pythontex}
  2. \begin{python} by \begin{pycode}

then compile with:

pdflatex file.tex
pythontex file.tex
pdflatex file.tex

Code:

\documentclass{article}
\usepackage[T1]{fontenc}% Must include this to print the underscores

%\usepackage{python}
\usepackage{pythontex}
\usepackage{graphicx}

\begin{document}

%\begin{python}
\begin{pycode}
import os
directory = "."
extension = ".png"
files = [file for file in os.listdir(directory) if 
file.lower().endswith(extension)]

for file in files:
   print(r"\begin{figure}[!ht]")
   print(r"\centering")
   print(r"\includegraphics[width=10cm,height=10cm]{%s}" % file)
   print(r"\caption{File \detokenize{%s}}" % file)
   print(r"\label{Serie}")
   print(r"\end{figure}")
\end{pycode}
%\end{python}

\end{document}

ForLoop

If you want another approach, then use the forloop package to include images sequentially. I, personally, prefer this method :)

\documentclass{article}

\usepackage{forloop}
\usepackage{graphicx}

\begin{document}

\newcounter{fig}% declare a counter

\forLoop{1}{3}{fig}{% loop from 1 to 3 with the counter defined above
% and execute this code in each iteration
  \begin{figure}[!ht]
  \centering
  \includegraphics[width=10cm,height=10cm]{./fig\the\value{fig}}
  \caption{File ./fig\the\value{fig}}
  \label{Serie}
  \end{figure}
}