【问题标题】:Create multiple pdf files from same tex file从同一个tex文件创建多个pdf文件
【发布时间】:2020-05-18 17:54:46
【问题描述】:

我有多个数据文件(result_a.csv、result_b.csv、..),我想为每个文件创建图(result_a.pdf、result_b.pdf - 或类似文件)。绘图相同,但输入文件不同,输出文件不同。有没有办法可以运行循环,从外部传递参数名称并使用独特的名称保存输出?

假设我创建情节的代码 -

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {result_\param.csv}; 
\end{semilogxaxis}
\end{tikzpicture}




\end{document}

【问题讨论】:

  • 如果您在 Linux 发行版或 Mac OS 下工作,您可以使用 shell 脚本来创建 result_a.tex、result_b.tex,...并在同一脚本中编译 tex 文件用于所需的输出 pdf。也许在 Windows 下你也可以采用类似的方法。
  • 谢谢@Eddymage,我正在使用mac os,请您提示我有关脚本的信息
  • 我会将其作为答案发布,以使其尽可能清晰。
  • 另一种可能的方法是只有一个 tex 文档循环所有 cvs 文件并使用 tikz externalise 库为每个绘图生成单独的文件。

标签: latex pdflatex


【解决方案1】:

假设您的文件名为document.tex,您可以通过使用以下命令编译将 csv 文件的名称传递给文档

pdflatex -jobname="document-b" "\\newcommand*\\version{b}\\input{document}"

和文件:

\documentclass[tikz]{standalone}

% setting a default value in case it is compiled without the newcommand
\unless\ifdefined\version
\def\version{a}
\fi


\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {result_\version.csv}; 
\end{semilogxaxis}
\end{tikzpicture}

\end{document}

【讨论】:

  • 太好了,我不知道pdflatex 的这个选项!尽管如此,您仍然需要一个循环(例如在 bash shell 脚本中)从不同的 .csv 文件创建不同的 tex 文件,对吧?
【解决方案2】:

如果您在 Linux Distro 或 Mac OS 下工作,您可以使用 shell 脚本。 让init.tex 成为带有

的 tex 文件
\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {

并让ends.tex 成为包含的文件

}; 
\end{semilogxaxis}
\end{tikzpicture}

以下脚本读取每个 .csv 文件,将其内容放入 tex 文件中,组织如下:

  • init.tex
  • result_x.csv
  • ends.tex

生成tex文件result_xx.tex并编译它,提供文件result_xx.pdf

#!/bin/bash

for i in $(ls *.csv)
do
    a=$(basename -s .csv $i)
    cp init.tex $a.tex
    echo $i >> $a.tex
    cat ends.tex >> $a.tex
    pdflatex $a.tex

done

这是一个幼稚的解决方案,有比我更有经验的 bash--SO 中的脚本编写者,但它应该可以工作。

PS。请记住使脚本可执行以使其工作:

chmod 755 myscript.sh

参见例如here

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2020-10-30
    • 2010-09-23
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多