自动创建变量名(合并字符串)

自动创建变量名(合并字符串)

我不知道如何解决我的问题。提供一点背景信息,以便您理解我为什么要这样做:我有一些包含数字的变量。因为这是不可能的(据我所知),所以我将它们命名为:VoltageI VoltageII 等。我现在想在 for 循环中使用 loopcounter 来处理它们。这对于我命名为 figure1 等的图形非常有效。->

includegraphics{path/figure\arabic{counter}}.

我的目标是用 I 等替换计数器(本例中为 1)。我已经编写了一条新命令:

\newcommand{\Romannum}{%
\ifnum #1=1
#1I
\fi
}

对于我文中的代码:

\Romannum{Profile}{1}

这打印给我:

ProfileI 

如预期。但我需要的输出不是文本,而是变量名称,以显示变量的内容->

\ProfileI

这可能吗或者有没有更简单的方法来获取变量名中的数字?

谢谢你!

这里有一个例子(由于缺少图像,它无法编译,但它使我的问题更加清晰。)

\documentclass{article} 

\usepackage{forloop}
\newcommand{\Romannum}[2]{%
\ifnum #2=1
#1I
\fi
 \ifnum #2=2
#1II
\fi
} 

\newcommand{\ProfileI}{Some text is written here.}
\newcommand{\ProfileII}{Some text is written here.}

\newcounter{profilecounter}
\setcounter{profilecounter}{1}

\begin{document} 
\forloop{profilecounter}{1}{\value{profilecounter} < 3}{%
\begin{figure}[htbp]
\centering
\includegraphics[width = \textwidth]{Profil\arabic{profilecounter}.png} %This works
\caption{Profile \arabic{profilecounter} -~\Romannum{Profile}{profilecounter}}%This doesnt work (it writes ProfileI. I'd need \ProfileI to acces the string in the variable.
 \end{figure}
 }

\end{document} } 

答案1

您可以使用例如expl3将计数器的当前值转换为罗马数字:

\documentclass{article}

\usepackage{forloop}
\newcommand{\ProfileI}{Some text for I is written here.}
\newcommand{\ProfileII}{Some text for II is written here.}

\newcounter{profilecounter}
\setcounter{profilecounter}{1}

\usepackage{xparse}
\ExplSyntaxOn 
 %changed to expandable to get it in the listoffigures.
\NewExpandableDocumentCommand\usecurrentProfile {}{ \use:c {Profile\int_to_Roman:n{ \value{profilecounter} }}}
\ExplSyntaxOff

\begin{document}
\listoffigures 
\forloop{profilecounter}{1}{\value{profilecounter} < 3}{%
\begin{figure}[htbp]
\centering
%graphic
\caption{Profile \arabic{profilecounter} -~\usecurrentProfile}%
 \end{figure}
 }

\end{document}

在此处输入图片描述

相关内容