변수 이름 자동 생성(병합 문자열)

변수 이름 자동 생성(병합 문자열)

내 문제를 해결하는 방법을 알 수 없습니다. 제가 왜 그런 일을 하는지 이해할 수 있도록 약간의 배경 정보를 제공합니다. 숫자를 포함하는 변수가 있습니다. (내가 아는 한) 이것은 불가능하기 때문에 나는 다음과 같이 이름을 지정했습니다: 전압I 전압II 등. 이제 루프 카운터를 사용하여 for 루프에서 처리하고 싶습니다. 이것은 내가 Figure1 등으로 명명한 그림에 적합합니다. ->

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

내 목표는 카운터(이 경우 1)를 I 등으로 대체하는 것입니다. 나는 이미 새로운 명령을 작성했습니다:

\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}

여기에 이미지 설명을 입력하세요

관련 정보