使用 Python 进行图形格式化

使用 Python 进行图形格式化

我正在尝试使用 python 包自动生成包含多个图像文件的图形。

我的输出看起来还不错,只是图片以单列形式输出,而如果我直接在 latex 中编写相同的代码,则输出结果为两列。有人能告诉我如何让 python 版本以双列形式输出图片吗?

这是我的代码:

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

python 循环输出

\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"}

如果我取出 python 输出并将其插入到 latex 中,则输出是双列:

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

有什么建议么?

答案1

Sigur 解释称,添加换行符会在输出中留下额外的空格,使其更宽。%行末的注释字​​符可防止添加这些空格。

例如

\documentclass{article}
\begin{document}

a
b
c

a%
b%
c

\end{document}

其中非空行仅包含可见字符,产生

示例输出

第一行中未注释的换行符会在输出中产生空格。

相关内容