# Automatically create variable names (merge strings)

i can't figure out how to solve my issue. A little bit Background info so you understand, why I would do such things: I have some variables that contain numbers. Because this is not possible (as far as i know) i did name then like: VoltageI VoltageII etc. I now want to process them in a for loop with the loopcounter. This works nice for figures i named figure1 etc. ->

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


My Goal is to Substitute the Counter (in this case 1) with a I and so on. I already wrote a new command:

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


For the Code in my text:

\Romannum{Profile}{1}


This prints me:

ProfileI


as intended. But I Need the Output not as a text but as a variable Name to Display the Content of the variable->

\ProfileI


Is this even possible or is there an easier way to get numbers in variable names?

Thank you!

Here an example (it doesnt compile due to the lack of the image but it makes my question more clear.)

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

• What does define \ProfileI then? – user31729 Apr 24 '17 at 10:53
• I'm not sure I understand the description you give, but in general, \csname text\endcsname, refers to a macro equivalent to \text. Likewise, \csname\Romannum{Profile}{1}\endcsname could be used to refer to a macro \ProfileI. – Steven B. Segletes Apr 24 '17 at 10:56
• \newcommand{\Romannum}[1]{% \ifnum\value{#1}=1 \csname #1I\endcsname \fi } if #1 will be the counter ? – user31729 Apr 24 '17 at 10:57
• Or easier: \newcommand{\Romannum}[1]{% \csname #1\Roman{#1}\endcsname }? -- Actually, I don't understand what you want to achieve here – user31729 Apr 24 '17 at 11:00
• \forloop is not defined. – Ulrike Fischer Apr 24 '17 at 11:44

You can use e.g. expl3 to convert the current value of your counter to a Roman number:

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