6

I'd like to manipulate the arguments in \newcommand. More explicitly, I'd like something like that:

\newcommand{\X}[1]{
#1+1
}

And if I call \X{0}, I want it to return 1 and not 0+1.

Thank you :-)

  • Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}). Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Claudio Fiandrino Jan 28 '13 at 08:54
  • 9
    Welcome to TeX.SE. Is your #1 argument always integer or decimal? You can also search for \numexpr on this site. – percusse Jan 28 '13 at 08:54
  • Related: http://tex.stackexchange.com/questions/36572 – Torbjørn T. Jan 28 '13 at 09:18
  • 1
    Welcome to TeX.sx! Why did you roll back your question? What part didn't you like in the edit? Deleting a greeting or a "thank you" in posts during edit might seem strange at first. Not saying "thank you" is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Once you have enough reputation, you can upvote the answers of the people who helped you. In this site upvoting is the preferred way here to say "thank you" to users who helped you. You can however say "thank you" in comment if you like. :-) – hpesoj626 Jan 28 '13 at 09:29

1 Answers1

9

This is the first time I tried \numexpr as suggested by percuße in comment. I have added \pgfmathparse as another possibility.

\documentclass[10pt]{article}

\usepackage{tikz}

\newcommand{\X}[1]{%
\pgfmathparse{int(#1+1)}\pgfmathresult%
}

\newcommand{\XX}[1]{%
\number\numexpr#1+1\relax%
}

\begin{document}
\X{0}
\begin{enumerate}
\foreach \x in {0,...,5}
    {\item \X{\x}}
\end{enumerate}
\XX{0}
\begin{enumerate}
\foreach \x in {0,...,5}
    {\item \XX{\x}}
\end{enumerate}
\end{document}

Note that you can also change \pgfmathparse{#1+1}\pgfmathresult into \pgfmathparse{int(#1+1)}\pgfmathresult if you want to remove the decimals.

hpesoj626
  • 17,282
  • I don't know if this adds any value to the link provided by @TorbjørnT. I have to go but please comment if that is the case. – hpesoj626 Jan 28 '13 at 09:23
  • 1
    I'd add some % at end of lines and warn about spurious spaces in the output – egreg Jan 28 '13 at 10:08