3

I have a Python code which detects my files. Now I will open it in Latex. But it seems not possible to give the detected names to Latex I have tried this:

\begin{pycode}
l = [f for f in os.listdir('path') if f.endswith('.jpg')]
\end{pycode}
\includegraphics[scale=0.1]{\py{l}}

Because it did not work I have used this:

\newcommand{\filename}{}
\edef\filename{l}

\includegraphics[scale=0.75]{\filename}

I need this because I have different files. They have filename that looks like this: part1_date, part1 is always the same only the date is variable. Therefore the day should be refered in the text (for example like the reference to a equation. And of course it must be printed.

Is it possble to do this with Pythontex? Or can I do this with another method?

Thanks

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Apr 24 '14 at 06:21
  • I have no idea about PythonTeX but filenames with _ characters are problematic, since _ is interpreted as a math mode command starting subscripts. –  Apr 24 '14 at 06:27
  • Thanks, I can change the style of the filenames. What pattern would be ok and how can I create a read in routine and extract the filename in parts- it is not important to use pythonTex,but it would be nice. I would prefer a method without using shell escape. – Overlord Apr 24 '14 at 07:36

1 Answers1

2

\py is not expandable. Therefore it cannot directly be used as file name for \includegraphics. But \py can be used to output TeX code that defines a macro. Then the macro contains the string and can be used for \includegraphics.

Example:

\documentclass{article}
\usepackage[a4paper,vmargin=2cm]{geometry}
\usepackage{pythontex}
\usepackage{graphicx}
\usepackage{url}
\usepackage{multido}

\begin{document}
\begin{pycode}
l = [f for f in os.listdir('../') if f.endswith('.jpg')]
\end{pycode}
\py{"\\def\\NumberOfFiles{" + "{}".format(len(l)) + "}"}
\providecommand*{\NumberOfFiles}{0}

\DeclareRobustCommand*{\PrintFile}[1]{%
  \expandafter\PrintFileAux\expandafter{\number#1}%
}
\newcommand*{\PrintFileAux}[1]{%
  \py{"\\path{" + l[#1] + "}"}%
}

\DeclareRobustCommand*{\DefFileName}[1]{%
  \expandafter\DefFileNameAux\expandafter{\number#1}%
}
\newcommand*{\DefFileNameAux}[1]{%
  \py{"\\def\\FileName{" + l[#1] + "}"}%
  \providecommand*{\FileName}{}%
}

\tableofcontents

\section{\NumberOfFiles\ file\ifnum\NumberOfFiles>1 s\fi}

\multido{\i=0+1}{\NumberOfFiles}{%
  \subsection{\PrintFile{\i}}%
  \DefFileName{\i}%
  \ifx\FileName\empty
  \else
    \includegraphics[height=\baselineskip]{\FileName}%
  \fi
}%
\end{document}

Result

Remarks:

  • The current directory is .., because python is executed in the auxiliary directory pythontex-files-<jobname> (<jobname> is the name of the main LaTeX file without extension).

  • If the path name contains special characters (such as _), then printing needs some more care. It can be printed as verbatim text. The example uses macro \path from package url.

  • The loop over all files can be done in different ways. The example implements the loop in LaTeX using \multido of package multido.

    Alternatively the loop can be implemented in Python by calling a LaTeX macro with the file name and/or date as argument.

  • The file name could be analyzed at TeX level to extract the date. The more comfortable way would be using Python to extract the date and convert it to the desired format.

Heiko Oberdiek
  • 271,626
  • Hi, thanks a lot for your help. This works really great. I work with Latex since a few weeks and the options of latex are very impressive.

    What would be better, making graphics in Matlab or in Latex by using pythontex? Overlord

    – Overlord Apr 24 '14 at 15:15
  • @Overlord: There are many, many ways and lots of programs to generate graphics. A starting point from the LaTeX side: What graphics packages are there for creating graphics in LaTeX documents? – Heiko Oberdiek Apr 24 '14 at 16:02
  • Heiko Oberdiek thank you. At the moment I work with networkX,matplotlib and pygraphviz. I use this for the additonal graphics. But the graphics which are the result of my research are my point of interest. The fastest way is to use matlab. The other one is using Latex. But I do not know if this make sense. – Overlord Apr 24 '14 at 16:11