0

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

  • Why not use Python to generate a .tex file with all you need and then simply input it using \input{}? – Sigur Feb 5 '18 at 12:44
  • @Sigur that's an idea, do you have any link where I can see an example? Thanks! – cicciodevoto Feb 5 '18 at 12:53
  • @cicciodevoto you can do that with almost any templating system available for python. The very simple Template module can do something similar. But it is probalby easier to make the LaTeX template an external file (external to python), and then have python fill in data into that file, and then save the generated TeX file to disk. It is worth checking into, saves a lot of time. – daleif Feb 5 '18 at 13:03
  • @daleif Thanks! I checked around and I am trying to use jijja2. If you have any suggestion please let me know. Thanks again! – cicciodevoto Feb 6 '18 at 9:33
1

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}
}
  • Phelype thanks for the answer! I don't understand what "compile with pythontex" means, I miss this step in the use of pythontex. Could you explain it to me? Thanks! – cicciodevoto Feb 5 '18 at 13:02
  • You just have to run pythontex your-file.tex from the command line like you would run pdflatex. I edited my answer to clarify that. – Phelype Oleinik Feb 5 '18 at 13:09
  • Phelype I followed your suggestions, but because the names of files have underscores, the first latex run and the pythontex run go well, but the second latex run doesn't understand them. Do you know how to solve it? – cicciodevoto Feb 6 '18 at 9:40
  • @cicciodevoto That's because of the caption. You are trying to use an underscore (_) in the text and, in normal typesetting environments, you should not be able to do that. But, since you are writing a file name, it should be okay, so we can make it work. You just have to \detokenize the file name and use a T1 font encoding to print the underscores correctly (\usepackage[T1]{fontenc}). I updated my answer with the modification. – Phelype Oleinik Feb 6 '18 at 10:41
  • Phelype Right the caption! Sorry I didn't realise. Thanks a lot! – cicciodevoto Feb 6 '18 at 11:44

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.