Code: Select all
\documentclass{article}
\begin{document}
\newcount\c
\newcommand\cream{\c=#1}
!
\end{document}
Code: Select all
\documentclass{article}
\begin{document}
\newcount\c
\newcommand\cream{\c=#1}
!
\end{document}
NEW: TikZ book now 40% off at Amazon.com for a short time.
\cream
. As an aside: \newcount
is a TeX command, the LaTeX way would be \newcounter
. Also \c
already has a definition: the cedilla.Code: Select all
\documentclass{article}
\newcounter{mycounter}
\newcommand*\cream[1]{\setcounter{mycounter}{#1}}
\begin{document}
\themycounter
\cream{4}
\themycounter
\end{document}
Code: Select all
\newcount\mycount
\def\cream#1{\mycount=#1 }% note the space after `#1'
\the\mycount
\cream{4}
\the\mycount
\bye
Code: Select all
\documentclass{article}
\begin{document}
\newcount\bekkerpage
\def\bl#1 #2 {%
#1\ifx&\else\kern1pt\fi#2\ifx&\else\,\fi%
\bekkerpage=#1%
}
\bl 202 b 2
\end{document}
You can but you should be aware thatLaurentius wrote:Thanks, Clemens. Can I use TeX code in LaTeX? I need to use \def rather than \newcommand.
\def
(and \newcount
btw) overwrites existing commands without warning. That's why LaTeX commands generally are preferred.Let's go through the code:Laurentius wrote:Something odd happens that I can't understand. Earlier it said "expected number" or something, and now,
TheCode: Select all
\documentclass{article} \begin{document} \newcount\bekkerpage \def\bl#1 #2 {% #1\ifx&\else\kern1pt\fi#2\ifx&\else\,\fi% \bekkerpage=#1% } \bl 202 b 2 \end{document}
\bekkerpage=#1
line causes the last '2' to disappear. I don't understand why.
Code: Select all
\def\bl#1 #2 {...}
\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
Code: Select all
\def\bl#1 #2 {#1\ifx&\else\kern1pt\fi#2\ifx&\else\,\fi\bekkerpage=#1}
Code: Select all
\bl 202 b 2
Code: Select all
202\ifx&202&\else\kern1pt\fi b\ifx&b&\else\,\fi\bekkerpage=2022
#1
\bekkerpage
gets assigned 2022
. This is where the 2
“disappears”.Code: Select all
\documentclass{article}
\begin{document}
\newcount\bekkerpage
\def\bl#1 #2 {%
#1\ifx&\else\kern1pt\fi#2\ifx&\else\,\fi%
\bekkerpage=#1%
}
\bl 202 b 2
bekkerpage: \the\bekkerpage
\end{document}
\bekkerpage=#1
(i.e. leave the %
out)
Code: Select all
\def\bl#1 #2 {%
#1\ifx&\else\kern1pt\fi#2\ifx&\else\,\fi
\bekkerpage=#1
}
\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&\else\kern1pt\fi#2\ifx&\else\,\fi
\bekkerpage=#1\relax
}
%
after \fi
is useless since spaces following a control sequence whose name consists of letters are ignored, anyway.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: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.
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}
Code: Select all
\test=1%
3
Code: Select all
\test=13
NEW: TikZ book now 40% off at Amazon.com for a short time.