自动查找表格或向量编写字幕

自动查找表格或向量编写字幕

我问这个问题是因为我找不到任何能直击要点的东西。

假设我们想要在 Latex 中自动绘制图形,显示某个变量在 5 个实验(数量可以变化)中的演变情况。这些实验被命名为“pX”。每个实验都有 3 个属性。我的问题如下:如何自动将每个实验的属性写入图表标题中?

此信息应在表格中或 4 个独立向量中找到。两种选项如下所示。

Exp   Atribute1   Atribute2 Atribute3
p1       X11        X12      X13
p2       X21        X22      X23
p7       X31        X32      X33
p9       X41        X42      X43
p14      X51        X52      X53  

\def \lexperim{1, 2, 7, 9, 14} 
\def \latrib1{X11, ..., X51}
\def \latrib2{X21, ..., X52}
\def \latrib3{X31, ..., X53}

为了使图形自动化,我定义了这个命令:

\newcommand\fig[1]{
\begin{figure}[h]
    \begin{center}
       \centering
       \captionsetup{justification=centering}
       \includegraphics[scale=0.45, keepaspectratio]{figures/#1-vari1.eps}
       \includegraphics[scale=0.45, keepaspectratio]{figures/#1-vari2.eps}
       \caption{Temporal evolution of the variable 1 for #1. HERE IS WHERE I WOULD LIKE TO PUT THE ATRIBUTES ASSOCIATED TO EACH EXPERIMENT} 
        \vspace*{-10pt}
       \label{fig:#1}
   \end{center}
\end{figure}
}

然后,我对实验次数进行循环。

\foreach \nexp in \lexperim{
    \fig{p\nexp}
 }

这很好,但如前所述,我想将该实验的属性放在标题中。为此,有两个选项:

  1. 进入表格,获取与实验相关的所有属性,并将它们与单位放在一个字符串中。这个选项对我来说最方便。但是,我找不到任何东西,也许是因为它相当复杂(尤其是对我这样的初学者来说)。因此,我也对第二个选项感到满意。

  2. 检测实验 pX 在向量 \lexperim 中的位置(假设这个位置是 k),并获取向量 \latrib1 \latrib2 \latrib3 的 k 分量。据我所知,这个简单的操作在 Latex 中并不简单。因此,我还想知道您是否建议我将其他工具的代码嵌入到 Latex 中以解决此类问题。

我很高兴阅读您的建议!

答案1

我可以提供一个expl3接口。

\documentclass{article}
\usepackage{xparse}
\usepackage{booktabs}

\ExplSyntaxOn

\prop_new:N \g_iggt_experiments_prop % save attributes
\seq_new:N \g_iggt_experiments_seq   % save experiment IDs
\tl_new:N \l__iggt_experiments_table_tl

\NewDocumentCommand{\addexperiment}{mmmm}
 {% #1 = experiment ID, #2 - #4 = three attributes
  \seq_gput_right:Nn \g_iggt_experiments_seq { #1 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@1 } { #2 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@2 } { #3 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@3 } { #4 }
 }

\NewDocumentCommand{\experimenttable}{o}
 {% #1 = nothing or list of IDs
  \tl_clear:N \l__iggt_experiments_table_tl
  \IfNoValueTF { #1 } { \__iggt_experiments_all: } { \__iggt_experiments_list:n { #1 } }
  \__iggt_experiments_print:
 }

\NewDocumentCommand{\printexperiment}{m}
 {% #1 = experiment ID
  Experiment\nobreakspace#1:~
  \prop_item:Nn \g_iggt_experiments_prop { #1@1 },~
  \prop_item:Nn \g_iggt_experiments_prop { #1@2 },~
  \prop_item:Nn \g_iggt_experiments_prop { #1@3 }.
 }

\cs_new_protected:Nn \__iggt_experiments_all:
 {
  \seq_map_function:NN \g_iggt_experiments_seq \__iggt_experiments_addrow:n
 }

\cs_new_protected:Nn \__iggt_experiments_list:n
 {
  \clist_map_function:nN { #1 } \__iggt_experiments_addrow:n
 }

\cs_new_protected:Nn \__iggt_experiments_addrow:n
 {
  \tl_put_right:Nx \l__iggt_experiments_table_tl
   {
    #1 &
    \prop_item:Nn \g_iggt_experiments_prop { #1@1 } &
    \prop_item:Nn \g_iggt_experiments_prop { #1@2 } &
    \prop_item:Nn \g_iggt_experiments_prop { #1@3 } \exp_not:N \\
   }
 }

\cs_new_protected:Nn \__iggt_experiments_print:
 {
  \begin{tabular}{@{}cccc@{}}
  \toprule
  Exp & Attr1 & Attr2 & Attr3 \\
  \midrule
  \tl_use:N \l__iggt_experiments_table_tl
  \bottomrule
  \end{tabular}
 }

\ExplSyntaxOff

\addexperiment{p1} {X11}{X12}{X13}
\addexperiment{p2} {X21}{X22}{X23}
\addexperiment{p7} {X31}{X32}{X33}
\addexperiment{p9} {X41}{X42}{X43}
\addexperiment{p14}{X51}{X52}{X53}

\begin{document}

\experimenttable

\bigskip

\experimenttable[p1,p7,p14]

\bigskip

\printexperiment{p2}

\end{document}

在此处输入图片描述

添加

下面展示了如何进行循环。

\documentclass{article}
\usepackage{xparse}
\usepackage{booktabs}

\ExplSyntaxOn

% a command for loops
\NewDocumentCommand{\foreachcsv}{mm}
 {
  \clist_map_inline:nn { #1 } { #2 }
 } 

% the specific part
\prop_new:N \g_iggt_experiments_prop % save attributes
\seq_new:N \g_iggt_experiments_seq   % save experiment IDs
\tl_new:N \l__iggt_experiments_table_tl

\NewDocumentCommand{\addexperiment}{mmmm}
 {% #1 = experiment ID, #2 - #4 = three attributes
  \seq_gput_right:Nn \g_iggt_experiments_seq { #1 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@1 } { #2 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@2 } { #3 }
  \prop_gput:Nnn \g_iggt_experiments_prop { #1@3 } { #4 }
 }

\NewDocumentCommand{\experimenttable}{o}
 {% #1 = nothing or list of IDs
  \tl_clear:N \l__iggt_experiments_table_tl
  \IfNoValueTF { #1 } { \__iggt_experiments_all: } { \__iggt_experiments_list:n { #1 } }
  \__iggt_experiments_print:
 }

\NewDocumentCommand{\printexperiment}{m}
 {% #1 = experiment ID
  Experiment\nobreakspace#1:~
  \prop_item:Nn \g_iggt_experiments_prop { #1@1 },~
  \prop_item:Nn \g_iggt_experiments_prop { #1@2 },~
  \prop_item:Nn \g_iggt_experiments_prop { #1@3 }.
 }

\cs_new_protected:Nn \__iggt_experiments_all:
 {
  \seq_map_function:NN \g_iggt_experiments_seq \__iggt_experiments_addrow:n
 }

\cs_new_protected:Nn \__iggt_experiments_list:n
 {
  \clist_map_function:nN { #1 } \__iggt_experiments_addrow:n
 }

\cs_new_protected:Nn \__iggt_experiments_addrow:n
 {
  \tl_put_right:Nx \l__iggt_experiments_table_tl
   {
    #1 &
    \prop_item:Nn \g_iggt_experiments_prop { #1@1 } &
    \prop_item:Nn \g_iggt_experiments_prop { #1@2 } &
    \prop_item:Nn \g_iggt_experiments_prop { #1@3 } \exp_not:N \\
   }
 }

\cs_new_protected:Nn \__iggt_experiments_print:
 {
  \begin{tabular}{@{}cccc@{}}
  \toprule
  Exp & Attr1 & Attr2 & Attr3 \\
  \midrule
  \tl_use:N \l__iggt_experiments_table_tl
  \bottomrule
  \end{tabular}
 }

\ExplSyntaxOff

% three lines commeted out just for the example
\newcommand{\experimentfigure}[1]{%
  \begin{figure}[htp]
    \centering
%    \captionsetup{justification=centering}
%    \includegraphics[scale=0.45, keepaspectratio]{figures/#1-vari1.eps}
%    \includegraphics[scale=0.45, keepaspectratio]{figures/#1-vari2.eps}
    \caption{Temporal evolution of the variable 1 for #1. \printexperiment{#1}}
    \label{fig:#1}
  \end{figure}
}


\addexperiment{p1} {X11}{X12}{X13}
\addexperiment{p2} {X21}{X22}{X23}
\addexperiment{p7} {X31}{X32}{X33}
\addexperiment{p9} {X41}{X42}{X43}
\addexperiment{p14}{X51}{X52}{X53}

\begin{document}

\section{Test}

\experimenttable

\bigskip

\experimenttable[p1,p7,p14]

\bigskip

\printexperiment{p2}

\section{Figures}

\foreachcsv{p1,p14}{\experimentfigure{#1}}

\end{document}

相关内容