![如何在 LaTex 中迭代嵌套的值列表](https://linux22.com/image/462380/%E5%A6%82%E4%BD%95%E5%9C%A8%20LaTex%20%E4%B8%AD%E8%BF%AD%E4%BB%A3%E5%B5%8C%E5%A5%97%E7%9A%84%E5%80%BC%E5%88%97%E8%A1%A8.png)
我的目标是使用 Latex 创建测量协议。由于它将用于软件创建协议,因此请尝试实现一些自动化。这意味着,我希望 LaTex 根据嵌套列表中的值创建文档的各个部分。因此,此列表存储了值列表,这些值将用于要创建的单个部分。
这是迄今为止我的代码的摘录:
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{svg}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xstring}
\usepackage{caption}
\usepackage{dashrule}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{nicefrac}
\usepackage{float}
\usepackage{lscape}
\usepackage{xcolor}
\usepackage[export]{adjustbox}
\usepackage{colortbl}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[format=plain]{caption}
\usepackage{xparse}
\usepackage{nicefrac}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=3.8cm]{geometry}
\renewcommand{\figurename}{Bild}
\renewcommand{\tablename}{Tabelle}
\definecolor{ArrangementClassA}{HTML}{c000c0}
\definecolor{ArrangementClassB}{HTML}{00c000}
\definecolor{ArrangementClassC}{HTML}{a52a2a}
\definecolor{ArrangementClassD}{HTML}{00c0c0}
\definecolor{ArrangementClassE}{HTML}{0000a0}
\ExplSyntaxOn
\NewDocumentCommand \CountItems { m } {
\clist_count:N #1
}
\NewDocumentCommand \CountInlineItems { m } {
\clist_count:n {#1}
}
\ExplSyntaxOff
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ImgDirPathArrangementClasses}{protocol_images/flake/arrangement_classes/}
\newcommand{\ImgDirPathSizeClasses}{protocol_images/flake/size_classes/}
\newcommand{\ImgCalibration}{0.785}
% Nested lists to store images and measurement results
\ExplSyntaxOn
\NewDocumentCommand{\DeclareListOfValues}{ m m }
{
\clist_gclear_new:c {g_list_of_values_#1_clist}
\clist_gset:cn {g_list_of_values_#1_clist} {#2}
}
\NewExpandableDocumentCommand{\GetValueFromList}{ m o m }
{
\IfValueTF { #2 }
{
\exp_args:Ne \clist_item:nn { \clist_item:cn {g_list_of_values_#1_clist} {#3} } { #2 }
}
{
\clist_item:cn {g_list_of_values_#1_clist} {#3}
}
}
\ExplSyntaxOff
%List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
\DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
%List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
\DeclareListOfValues{GraphiteArrangementClasses}{
{14-1-11,73.6,26.4,0,0,0},
{15-1-4,72.7,27.3,0,0,0},
{15-1-6,67.7, 32.3,0,0,0},
{16-1-10,75.6, 24.4,0,0,0},
{1562-3-6,68.7,31.3,0,0,0},
{1562-3-16,71.6,28.4,0,0,0},
{1563-3-8,85.9,14.1,0,0,0},
{1563-3-13,71.8,28.2,0,0,0},
{1564-2-7,58.1,41.9,0,0,0},
{1564-3-14,56.2,43.8,0,0,0}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End Document variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Auswertung nach Graphitanordnung gemäß DIN EN ISO 945}
\subsection{Gesamtauswertung}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
/pgf/number format/1000 sep={},
width=.7\linewidth,
height=1.4in,
enlarge y limits={upper, value=0.2},
scale only axis,
clip=false,
separate axis lines,
axis on top,
xmin=0,
xmax=6,
xtick={1,2,3,4,5},
x tick style={draw=none},
xticklabels={A,B,C,D,E},
ytick={0,20,40,60,80,100},
ymin=0,
ymax=100,
ylabel={Häufigkeit $[\%]$},
xlabel={Anordnungsklassen},
every axis plot/.append style={
ybar,
bar width=32.0,
bar shift=0pt,
fill
},
title=\textbf{Graphitanordnung - Gesamtergebnis},
nodes near coords
]
\addplot[fill=ArrangementClassA, draw=ArrangementClassA]coordinates {(1,\GetValueFromList{GraphiteArrangementClassesTotalRes}{1})};
\addplot[fill=ArrangementClassB, draw=ArrangementClassB]coordinates{(2,\GetValueFromList{GraphiteArrangementClassesTotalRes}{2})};
\addplot[fill=ArrangementClassC, draw=ArrangementClassC]coordinates{(3,\GetValueFromList{GraphiteArrangementClassesTotalRes}{3})};
\addplot[fill=ArrangementClassD, draw=ArrangementClassD]coordinates{(4,\GetValueFromList{GraphiteArrangementClassesTotalRes}{4})};
\addplot[fill=ArrangementClassE, draw=ArrangementClassE]coordinates{(5,\GetValueFromList{GraphiteArrangementClassesTotalRes}{5})};
\end{axis}
\end{tikzpicture}
\end{figure}
\subsection{Einzelbildauswertungen}
% HOW TO GET VALUES FROM THE LIST - EXAMPLE
% \GetValueFromList{GraphiteArrangementClasses}{1}\\ % 14-1-11,73.6,26.4,0,0,0
% \GetValueFromList{GraphiteArrangementClasses}[1]{1}\\ % 14-1-11
% \GetValueFromList{GraphiteArrangementClasses}[2]{1}\\ % 73.6
% \GetValueFromList{GraphiteArrangementClasses}[3]{1}\\ % 26.4
% \GetValueFromList{GraphiteArrangementClasses}[4]{1}\\ % 0
% \GetValueFromList{GraphiteArrangementClasses}[5]{1}\\ % 0
% \GetValueFromList{GraphiteArrangementClasses}[6]{1}\\ % 0
% Concatenating the image path
\def\ImgPath{\ImgDirPathArrangementClasses}
\global\edef\ImgPath{\ImgPath\GetValueFromList{GraphiteArrangementClasses}[1]{1}}
\begin{minipage}[t]{0.6\linewidth}\vspace{0pt}
\begin{figure}[H]
\boxed{\includegraphics[width=\linewidth]{\ImgPath}}
\caption{\GetValueFromList{GraphiteArrangementClasses}[1]{1}}
\end{figure}
\end{minipage}~~~
\begin{minipage}[t]{0.35\linewidth}\vspace{0pt}
\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[
/pgf/number format/1000 sep={},
width=0.9\linewidth,
height=1.0in,
enlarge y limits={upper, value=0.2},
scale only axis,
clip=false,
separate axis lines,
axis on top,
xmin=0,
xmax=6,
xtick={1,2,3,4,5},
x tick style={draw=none},
xticklabels={A,B,C,D,E},
ytick={0,20,40,60,80,100},
ymin=0,
ymax=100,
xlabel={Anordnungsklassen},
every axis plot/.append style={
ybar,
bar width=12.0,
bar shift=0pt,
fill
},
title=\textbf{Graphitanordnung - Einzelbild},
nodes near coords,
every node near coord/.append style={rotate=90, anchor=west}
]
\addplot[fill=ArrangementClassA, draw=ArrangementClassA]coordinates {(1,\GetValueFromList{GraphiteArrangementClasses}[2]{1})};
\addplot[fill=ArrangementClassB, draw=ArrangementClassB]coordinates{(2,\GetValueFromList{GraphiteArrangementClasses}[3]{1})};
\addplot[fill=ArrangementClassC, draw=ArrangementClassC]coordinates{(3,\GetValueFromList{GraphiteArrangementClasses}[4]{1})};
\addplot[fill=ArrangementClassD, draw=ArrangementClassD]coordinates{(4,\GetValueFromList{GraphiteArrangementClasses}[5]{1})};
\addplot[fill=ArrangementClassE, draw=ArrangementClassE]coordinates{(5,\GetValueFromList{GraphiteArrangementClasses}[6]{1})};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{minipage}
\end{document}
我现在想要做的是为每个子列表自动创建子部分(Einzelbildauswertungen)石墨排列类别不幸的是我到目前为止还无法让它工作,因为 \foreach 中的列表作为参数不起作用。
非常感谢任何能帮助解决此问题的帮助!
答案1
您只需使用常规的 TeX 循环即可:
\documentclass[10pt,a4paper]{article}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=3.8cm]{geometry}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{float}
\renewcommand{\figurename}{Bild}
\renewcommand{\tablename}{Tabelle}
\definecolor{ArrangementClassA}{HTML}{c000c0}
\definecolor{ArrangementClassB}{HTML}{00c000}
\definecolor{ArrangementClassC}{HTML}{a52a2a}
\definecolor{ArrangementClassD}{HTML}{00c0c0}
\definecolor{ArrangementClassE}{HTML}{0000a0}
\ExplSyntaxOn
\NewDocumentCommand \CountItems { m } {
\clist_count:N #1
}
\NewDocumentCommand \CountInlineItems { m } {
\clist_count:n {#1}
}
\ExplSyntaxOff
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ImgDirPathArrangementClasses}{protocol_images/flake/arrangement_classes/}
\newcommand{\ImgDirPathSizeClasses}{protocol_images/flake/size_classes/}
\newcommand{\ImgCalibration}{0.785}
% Nested lists to store images and measurement results
\ExplSyntaxOn
\NewDocumentCommand{\DeclareListOfValues}{ m m }
{
\clist_gclear_new:c {g_mahakala_list_of_values_#1_clist}
\clist_gset:cn {g_mahakala_list_of_values_#1_clist} {#2}
}
\NewExpandableDocumentCommand{\GetValueFromList}{ m o m }
{
\IfValueTF { #2 }
{
\exp_args:Ne \clist_item:nn { \clist_item:cn {g_mahakala_list_of_values_#1_clist} {#3} } { #2 }
}
{
\clist_item:cn {g_mahakala_list_of_values_#1_clist} {#3}
}
}
\ExplSyntaxOff
%List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
\DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
%List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
\DeclareListOfValues{GraphiteArrangementClasses}{
{14-1-11,73.6,26.4,0,0,0},
{15-1-4,72.7,27.3,0,0,0},
{15-1-6,67.7, 32.3,0,0,0},
{16-1-10,75.6, 24.4,0,0,0},
{1562-3-6,68.7,31.3,0,0,0},
{1562-3-16,71.6,28.4,0,0,0},
{1563-3-8,85.9,14.1,0,0,0},
{1563-3-13,71.8,28.2,0,0,0},
{1564-2-7,58.1,41.9,0,0,0},
{1564-3-14,56.2,43.8,0,0,0}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End Document variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Auswertung nach Graphitanordnung gemäß DIN EN ISO 945}
\subsection{Gesamtauswertung}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
/pgf/number format/1000 sep={},
width=.7\linewidth,
height=1.4in,
enlarge y limits={upper, value=0.2},
scale only axis,
clip=false,
separate axis lines,
axis on top,
xmin=0,
xmax=6,
xtick={1,2,3,4,5},
x tick style={draw=none},
xticklabels={A,B,C,D,E},
ytick={0,20,40,60,80,100},
ymin=0,
ymax=100,
ylabel={Häufigkeit $[\%]$},
xlabel={Anordnungsklassen},
every axis plot/.append style={
ybar,
bar width=32.0pt,
bar shift=0pt,
fill
},
title=\textbf{Graphitanordnung - Gesamtergebnis},
nodes near coords
]
\addplot[fill=ArrangementClassA, draw=ArrangementClassA] coordinates {(1,\GetValueFromList{GraphiteArrangementClassesTotalRes}{1})};
\addplot[fill=ArrangementClassB, draw=ArrangementClassB] coordinates {(2,\GetValueFromList{GraphiteArrangementClassesTotalRes}{2})};
\addplot[fill=ArrangementClassC, draw=ArrangementClassC] coordinates {(3,\GetValueFromList{GraphiteArrangementClassesTotalRes}{3})};
\addplot[fill=ArrangementClassD, draw=ArrangementClassD] coordinates {(4,\GetValueFromList{GraphiteArrangementClassesTotalRes}{4})};
\addplot[fill=ArrangementClassE, draw=ArrangementClassE] coordinates {(5,\GetValueFromList{GraphiteArrangementClassesTotalRes}{5})};
\end{axis}
\end{tikzpicture}
\end{figure}
\newcounter{iterationindex}
\loop
\stepcounter{iterationindex}
\subsection{Einzelbildauswertungen}
% HOW TO GET VALUES FROM THE LIST - EXAMPLE
% \GetValueFromList{GraphiteArrangementClasses}{1}\\ % 14-1-11,73.6,26.4,0,0,0
% \GetValueFromList{GraphiteArrangementClasses}[1]{1}\\ % 14-1-11
% \GetValueFromList{GraphiteArrangementClasses}[2]{1}\\ % 73.6
% \GetValueFromList{GraphiteArrangementClasses}[3]{1}\\ % 26.4
% \GetValueFromList{GraphiteArrangementClasses}[4]{1}\\ % 0
% \GetValueFromList{GraphiteArrangementClasses}[5]{1}\\ % 0
% \GetValueFromList{GraphiteArrangementClasses}[6]{1}\\ % 0
% Concatenating the image path
\def\ImgPath{\ImgDirPathArrangementClasses}
\global\edef\ImgPath{\ImgPath\GetValueFromList{GraphiteArrangementClasses}[1]{\theiterationindex}}
\begin{minipage}[t]{0.6\linewidth}\vspace{0pt}
\begin{figure}[H]
%\boxed{\includegraphics[width=\linewidth]{\ImgPath}}
\boxed{\includegraphics[width=\linewidth, height=4cm]{example-image-a}}
\caption{\GetValueFromList{GraphiteArrangementClasses}[1]{\theiterationindex}}
\end{figure}
\end{minipage}~~~
\begin{minipage}[t]{0.35\linewidth}\vspace{0pt}
\begin{figure}[H]
\begin{tikzpicture}
\pgfmathtruncatemacro{\currentindex}{\theiterationindex}
\begin{axis}[
/pgf/number format/1000 sep={},
width=0.9\linewidth,
height=1.0in,
enlarge y limits={upper, value=0.2},
scale only axis,
clip=false,
separate axis lines,
axis on top,
xmin=0,
xmax=6,
xtick={1,2,3,4,5},
x tick style={draw=none},
xticklabels={A,B,C,D,E},
ytick={0,20,40,60,80,100},
ymin=0,
ymax=100,
xlabel={Anordnungsklassen},
every axis plot/.append style={
ybar,
bar width=12.0pt,
bar shift=0pt,
fill
},
title=\textbf{Graphitanordnung - Einzelbild},
nodes near coords,
every node near coord/.append style={rotate=90, anchor=west}
]
\addplot[fill=ArrangementClassA, draw=ArrangementClassA] coordinates {(1,\GetValueFromList{GraphiteArrangementClasses}[2]{\theiterationindex})};
\addplot[fill=ArrangementClassB, draw=ArrangementClassB] coordinates {(2,\GetValueFromList{GraphiteArrangementClasses}[3]{\theiterationindex})};
\addplot[fill=ArrangementClassC, draw=ArrangementClassC] coordinates {(3,\GetValueFromList{GraphiteArrangementClasses}[4]{\theiterationindex})};
\addplot[fill=ArrangementClassD, draw=ArrangementClassD] coordinates {(4,\GetValueFromList{GraphiteArrangementClasses}[5]{\theiterationindex})};
\addplot[fill=ArrangementClassE, draw=ArrangementClassE] coordinates {(5,\GetValueFromList{GraphiteArrangementClasses}[6]{\theiterationindex})};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{minipage}
\ifnum\theiterationindex<10\repeat
\end{document}
一些说明:
- 您不应该在序言中多次加载包,因为可能会导致错误并减慢编译过程。
bar width
至少在当前 PGFplots 版本中需要一个维度。我将其添加pt
到给定的值。- 由于我无法访问图片,因此我
\boxed{\includegraphics[width=\linewidth]{\ImgPath}}
在您的代码中用替换了\boxed{\includegraphics[width=\linewidth, height=4cm]{example-image-a}}
。