# Number as Argument for Command

I have defined the following:

\newcounter{testCounter}
\newcommand{\testCommand}[1]{
\setcounter{testCounter}{#1}
}


Now I want to call this command in the document:

\begin{document}
\testCommand{12}
\end{document}


I was hoping for this to give me:

However, I get the Error ,

"There were undefined references."

Can someone explain to me why this wont work? It seems pretty straighforward... I am using the following packages also:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{marvosym}
\usepackage{lmodern}
\usepackage{parskip}
\usepackage{pifont}
\usepackage{calc}
\usepackage{nameref}


Thanks!

• It's a warning, not an error, but you're using the wrong way. \ref{testcounter} is trying to use a label named testcounter which does not exist. – user31729 Jan 2 '18 at 22:53
• Did you compiled twice? – Martin Scharrer Jan 2 '18 at 22:54
• I see, but I thought you reference a counter the same way, no? – Douglas James Bock Jan 2 '18 at 22:55
• AFAIK you need to use \refstepcounter to get a referencible counter. Maybe try to set, add -1 and then use refstep. – Martin Scharrer Jan 2 '18 at 22:55
• \ref{xyz} needs a \label{xyz} somewhere. – Ulrike Fischer Jan 2 '18 at 22:57

You want to print the number that is stored in a counter: Use \thetestCounter or \arabic{testCounter}

Any counter defined with \newcounter gets an associated macro \theX, which defaults to \arabic{X}, i.e. \newcounter{foo} defines \thefoo, printing the counter value with arabic figures.

If a reference should be used, another approach is needed, see \otherCommand, where the counter is set to a value reduced by one, then \refstepcounter is applied, immediately use a \label to 'freeze the counter reference value.

This is written to the .aux file and can be loaded in the 2nd run and referred to right from the start!

\documentclass{article}

\newcommand{\myconstant}{4}

\newcounter{testCounter}
\newcommand{\testCommand}[1]{%
\setcounter{testCounter}{#1}%
}

\newcommand{\otherCommand}[1]{%
%Does not need calc package
\setcounter{testCounter}{\numexpr#1-1}%
\refstepcounter{testCounter}%
}

\begin{document}
Later on, you will set the counter to \ref{somelabel}

\testCommand{12}

\testCommand{\numexpr12*\myconstant}

\otherCommand{1001}\label{somelabel}

\end{document}


• the \arabic{...} was what I needed. But say I now needed to multiply this counter by, say, 3. How could I achieve that? – Douglas James Bock Jan 2 '18 at 23:13
• @DouglasJamesBock: That's another question. Do you want to multiple the counter value or its printed value? – user31729 Jan 2 '18 at 23:14
• I understand it's a different question. I was hoping that once I fix the problem in the question this would resolve, too. It didn't... I want to multiply the counter value by some constant. – Douglas James Bock Jan 2 '18 at 23:17
• \testCommand{\numexpr 12*3}, for example. \numexpr evaluates the calculation – user31729 Jan 2 '18 at 23:18
• @DouglasJamesBock: You can perform calculations on arguments using xfp. See my answer. – Werner Jan 2 '18 at 23:18

\references work with \labels, so in order to use any \ref, you must have an accompanying \label somewhere. Moreover, \label grabs only specific kinds of referencable objects (yes, mostly counters, but those that have been "adjusted in a specific way"; see Understanding how references and labels work).

In your example, it is simpler to use

\newcommand{\testCommand}[1]{%
}


without a \ref. If you want to perform calculations on your number, you can use a package for this. Perhaps something like

\documentclass{article}

\usepackage{xfp}

\newcommand{\testCommand}[1]{%
}

\begin{document}

\testCommand{12}

\testCommand{12 * 3}

\testCommand{12 / 4}

\end{document}
`