7

I'm trying to use the python package to automate figure generation for figures containing multiple image files.

My output seems okay, except that the imgaes are coming out as a single column, whereas if I write the same code in straight latex it comes out as two columns. Can anyone tell me how to make the python version output the figures as a double column?

here is my code:

\documentclass{article}
\usepackage{graphicx,python}

\begin{document}
\begin{figure}
\begin{python}
import os
directory = "."
extension = ".png"
files = [file for file in os.listdir(r'D:\New folder\Sean Read\visualisation examples\normalised cross sections\NC04\Axial') if file.lower().endswith("png")]
name=[]
for file in files:
    fileName, fileExtension = os.path.splitext(file)
    name.append(fileName)
for file in name:
   print(r'\includegraphics[width=0.4\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/%s"}' % file)
\end{python}
\end{figure}
\end{document}

the python loop puts out

\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/10acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/11acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/1acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/2acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/3acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/4acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/5acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/6acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/7acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/8acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/9acs copy"}

If I take out the python output and insert it in the latex the output is double columns:

\documentclass{article}
\usepackage{graphicx,python}

\begin{document}
\begin{figure}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/10acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/11acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/1acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/2acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/3acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/4acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/5acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/6acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/7acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/8acs copy"}
\includegraphics[width=0.5\textwidth]{"D:/New folder/Sean Read/visualisation examples/normalised cross sections/NC04/Axial/9acs copy"}
\end{figure}
\end{document}

Any suggestions?

  • Maybe you need some spacing adjustments. Since you want each figure as half of text width, I suggest you to use \begin{figure}\centering and also I would insert % at the end of each line after closing \includegraphics. – Sigur May 14 '15 at 6:14
  • Hey thanks, the added % seems to have done the trick, why does this help? – sean read May 14 '15 at 6:49
  • 3
    When you break the code into two lines some additional horizontal space could appear between them. If you insert % just after the last char in the line then you make sure that the following line will be executed exactly after the last command in the previous line. – Sigur May 14 '15 at 6:56
  • @Sigur You should write an answer – Andrew Swann Jun 22 '16 at 7:24
4

As Sigur explains, adding newlines places extra spaces in the output, making it wider. A comment character % at the end of the lines prevents these spaces being added.

For example

\documentclass{article}
\begin{document}

a
b
c

a%
b%
c

\end{document}

where non-blank lines contain only the visible characters, produces

Sample output

with the uncommented line breaks in the first lines producing spaces in the output.

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.