Как перебрать вложенный список значений в LaTex

Как перебрать вложенный список значений в LaTex

Моя цель — создать протокол измерений с использованием 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}}.

Связанный контент