Generalread parameter as number

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

read parameter as number

Post by Laurentius »

Is it possible to read a parameter as a number?

Code: Select all

\documentclass{article}
\begin{document}
\newcount\c
\newcommand\cream{\c=#1}
!
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

read parameter as number

Post by cgnieder »

You need to specify the number of arguments to \cream. As an aside: \newcount is a TeX command, the LaTeX way would be \newcounter. Also \c already has a definition: the cedilla.

Here's a LaTeX way how you could do it:

Code: Select all

\documentclass{article}
\newcounter{mycounter}
\newcommand*\cream[1]{\setcounter{mycounter}{#1}}
\begin{document}

\themycounter

\cream{4}

\themycounter

\end{document}
Here's the plain TeX way:

Code: Select all

\newcount\mycount
\def\cream#1{\mycount=#1 }% note the space after `#1'

\the\mycount

\cream{4}

\the\mycount

\bye
Regards
site moderator & package author
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

read parameter as number

Post by Laurentius »

Thanks, Clemens. Can I use TeX code in LaTeX? I need to use \def rather than \newcommand. Something odd happens that I can't understand. Earlier it said "expected number" or something, and now,

Code: Select all

\documentclass{article}
\begin{document}
\newcount\bekkerpage
\def\bl#1 #2 {%
	#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi%
	\bekkerpage=#1%
	}
\bl 202 b 2
\end{document}
The \bekkerpage=#1 line causes the last '2' to disappear. I don't understand why.

L.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

read parameter as number

Post by cgnieder »

Laurentius wrote:Thanks, Clemens. Can I use TeX code in LaTeX? I need to use \def rather than \newcommand.
You can but you should be aware that \def (and \newcount btw) overwrites existing commands without warning. That's why LaTeX commands generally are preferred.
Laurentius wrote:Something odd happens that I can't understand. Earlier it said "expected number" or something, and now,

Code: Select all

\documentclass{article}
\begin{document}
\newcount\bekkerpage
\def\bl#1 #2 {%
	#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi%
	\bekkerpage=#1%
	}
\bl 202 b 2
\end{document}
The \bekkerpage=#1 line causes the last '2' to disappear. I don't understand why.
Let's go through the code:

Code: Select all

\def\bl#1 #2 {...}
This defines \bl with two delimited arguments. The first is everything up to the first space, the second everything up to the second space. The delimiting spaces are discarded when the macro reads its arguments.

Code: Select all

\bl 202 b 2
202 is #1 and b is #2

Now I rewrite the macro definition without indentation to see how it is put into the input stream:

Code: Select all

\def\bl#1 #2 {#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi\bekkerpage=#1}
The call

Code: Select all

\bl 202 b 2
now is equivalent to:

Code: Select all

202\ifx&202&\else\kern1pt\fi b\ifx&b&\else\,\fi\bekkerpage=2022
You see that since there is no space after the last #1 \bekkerpage gets assigned 2022. This is where the 2 “disappears”.
You can verify this:

Code: Select all

\documentclass{article}
\begin{document}
\newcount\bekkerpage
\def\bl#1 #2 {%
	#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi%
	\bekkerpage=#1%
	}
\bl 202 b 2

bekkerpage: \the\bekkerpage
\end{document}
You have two choices: either leave a space after \bekkerpage=#1 (i.e. leave the % out)

Code: Select all

\def\bl#1 #2 {%
	#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi
	\bekkerpage=#1
	}
or add a \relax which also prevents the count from scanning ahead to look if the number continues; this is probably the safer way:

Code: Select all

\def\bl#1 #2 {%
	#1\ifx&#1&\else\kern1pt\fi#2\ifx&#2&\else\,\fi
	\bekkerpage=#1\relax
	}
BTW: the % after \fi is useless since spaces following a control sequence whose name consists of letters are ignored, anyway.

Regards
site moderator & package author
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: read parameter as number

Post by Laurentius »

Thanks alot for taking the time to reply so thoroughly. I can do what I wanted to now, though I still don't quite understand why #1 isn't just what comes before the first space. I would expect the space to be important when the command is called, not inside its definition. But this isn't important.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

read parameter as number

Post by cgnieder »

Laurentius wrote:I still don't quite understand why #1 isn't just what comes before the first space. I would expect the space to be important when the command is called, not inside its definition. But this isn't important.
It has nothing to do with the macro definition it's just the how count assignments in plain TeX work (and another reason why generally LaTeX syntax is preferred in LaTeX). They scan ahead looking if a number continues or not. This is stopped by a space or any other non-expandable token. Try this:

Code: Select all

\documentclass{article}
\begin{document}

\newcount\test

\test=1
2

test: \the\test

\test=1%
3

Huh? Where's the ``3''?

test: \the\test

\end{document}
Remember that

Code: Select all

\test=1%
3
is the same as

Code: Select all

\test=13
Regards
site moderator & package author
Post Reply