4

For making the following mwe work I obviously (so I thought, but that is not true, see answers) have to convert the macro \i to a counter. But how?

\documentclass{scrartcl}
\usepackage{romannum}
\usepackage{tikz}
\begin{document}
 \foreach \i in {1,2,3} \Romannum{\i};
\end{document}
8

You don't have to make it a counter, but you have to use another variable name for the loop:

\documentclass{scrartcl}
\usepackage{romannum}
\usepackage{tikz}
\begin{document}
\foreach \x in {1,2,3} \Romannum{\x};
\end{document}
| improve this answer | |
  • I am a bit confused: Why is \x handled in a different way than \i? – Jürgen Feb 27 '18 at 9:59
  • 1
    The macro \i is already defined in LaTeX (it's a dotless i: ı). It can be used in a \foreach, but only if it is expanded at the right time, such as in the answer by @skillmon (otherwise the original definition is used, which results in an error). – Marijn Feb 27 '18 at 10:30
  • Oh yes, I remember reading about the \i. It never happened to produce problems in \foreach so far, but I will avoid it in the future. Thanks for the solution and the explanation. – Jürgen Feb 27 '18 at 10:37
7

You don't have to make it a counter, but you have to expand \i before \Romannum:

\documentclass{scrartcl}
\usepackage{romannum}
\usepackage{tikz}
\begin{document}
 \foreach \i in {1,2,3} \expandafter\Romannum\expandafter{\i};
\end{document}
| improve this answer | |
  • Works fine, but Marijn's solution is shorter. ;-) Thanks anyway. – Jürgen Feb 27 '18 at 9:59
3

I'm not very fond of \Romannum, which uses \MakeUppercase, making it unexpandable.

Here's an expandable version:

\documentclass{scrartcl}

\makeatletter
\newcommand{\Romannum}[1]{\expandafter\@slowromancap\romannumeral#1@}
\makeatother

\usepackage{tikz}
\begin{document}
 \foreach \i in {1,2,3} {\Romannum{\i};}
\end{document}

It could be easier doing \let\Romannum\@Roman, but there are babel languages that change \@Roman.

Another strategy is using expl3:

\documentclass{scrartcl}

\usepackage{xparse}

\ExplSyntaxOn
\cs_new_eq:NN \Romannum \int_to_Roman:n
\ExplSyntaxOff

\usepackage{tikz}
\begin{document}
 \foreach \i in {1,2,3} {\Romannum{\i};}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.