2

I have defined two variables

\def\aa{3}  \def\bb{1}

\def\qq{\multiply\aa by \bb}

but when I try to use \qq in a \matrix command like

\matrix[matrix of math nodes]{12 & \qq\\};

ktikz says:

temptikzcode.pgf:60 You can't use `the character 3'

what am I doing wrong?

I suspect that there is a type of variable problem, but I couldn't solve it by means \print{\qq} or \string\qq.

Thanks

Torbjørn T.
  • 206,688
JOM
  • 457
  • \multiply\aa by \bb is wrong if \aa is no count register. –  Jun 10 '16 at 16:00
  • A comment off-topic: You have answers to your other questions, but did not accept them -- if they do what you requested, consider to accept them. –  Jun 10 '16 at 16:03
  • See my answer please –  Jun 10 '16 at 18:56
  • Don't overwrite existing macros unless you are absolutely sure you know what you are doing and why! – cfr Jun 30 '16 at 23:51

3 Answers3

4

A \def\aa{3} does not allocate a count register. \multiply requires a count register (an 'integer variable').

Either use count registers or \the\numexpr \aa *\bb, the later works for integer numbers only, whereas \multiply can be used for numeric variables too.

The example uses both way.

\multiply\mynumberone by \mynumbertwo will multiply the contents of both count registers and store the result in \mynumberone, which must either printed with \the\mynumberone then or with \number\value{mynumberone}.

The \numexpr - approach is easier, but requires e-TeX, but this shouldn't be an issue nowadays.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}


\def\aa{3}  \def\bb{1}

\newcount\mynumberone
\newcount\mynumbertwo

\mynumberone=3
\mynumbertwo=2

\def\qq{\the\numexpr\aa * \bb\relax}

\def\ww{\multiply\mynumberone by \mynumbertwo}


\begin{tikzpicture}
  \matrix[matrix of math nodes]{12 & \qq\\ 15 & \ww\the\mynumberone\\};
\end{tikzpicture}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Alternatively, use \let\aa\mynumberone etc. –  Jun 10 '16 at 16:01
  • Thanks @Christian. For your explanation and example. I think is better to me right now not to do it with counters. – JOM Jun 10 '16 at 19:15
  • @JOM: That's what counters made for -- for integer entries!! –  Jun 10 '16 at 19:17
  • I apologize. Thinking that I have three (3x3) matrixes, 27 counters that never "step". – JOM Jun 30 '16 at 22:42
  • Is it wise to overwrite an existing macro, even if the OP does? – cfr Jun 30 '16 at 23:52
  • I don't understand that, @cfr (and translators to spanish doesn't work here) – JOM Jul 01 '16 at 23:12
  • @JOM: cfr is just nitpicking about using \qq etc. as macro names. Apparently, those names are used already in other context, but I never used them before, so they don't seem to be really important –  Jul 01 '16 at 23:16
  • @JOM I don't use ß but it would hardly be nit-picking to suggest that overwriting \ss is unwise. German may not need å, but German is not the only language non-nit-pickers may wish to write. But apparently those languages just aren't important. – cfr Jul 02 '16 at 01:13
4

A working method would be to use \pgfmathtruncatemacro instead of \multiply. This will calculate the result, and truncate the decimals. If you need decimals, you can use \pgfmathsetmacro.

As an aside, one disadvantage of using \def as opposed to \newcommand is that it won't tell you if you're redefining an existing macro. In this case, \aa is the macro for printing an "å". In general, redefining existing macros is not good practice, unless you, to quote cfr, "are absolutely sure you know what you are doing and why".

Unfortunately, \pgfmathtruncatemacro and \pgfmathsetmacro do not check for existence of the macro either.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
%\newcommand\aa{3}  % causes error, command \aa already defined
\newcommand\aaa{3} 
\newcommand\bb{1}
\pgfmathtruncatemacro{\qq}{\aaa*\bb}
\usetikzlibrary{matrix}
\begin{document} 
\begin{tikzpicture}
\matrix[matrix of math nodes]{12 & \qq\\};
\end{tikzpicture}
\end{document} 
Torbjørn T.
  • 206,688
  • Any reference on how to handle variables in TikZ? – Atcold Feb 17 '22 at 07:13
  • @Atcold Can you be more specific? Pgfmathsetmacro etc. are described in the manual. I can't remember the chapter, but just search in the PDF and you'll find it. You can also look up declare function which can be useful in some cases. – Torbjørn T. Feb 17 '22 at 07:33
  • I've clarified my question here https://tex.stackexchange.com/q/634141/33287 – Atcold Feb 17 '22 at 07:34
0

It is just for comment that my original problem was to put two matrixes and perform the product in three different ways to generate a sequence to make a video for my students. Node backgrounds and arrows were used intensely.

I think I solve other issue that perhaps somebody has interest: to put all entries of a matrix to the right. (like\begin{array}{rrr}... )

\pgfmathtruncatemacro{\qc}{\ha*\bc} \def\qct{{\color{c1}\ifthenelse{\qc<0}{\qc}{\D\qc}}}

where \qc and \ha are calculated variables, \bc is the B(1,3) entry of my second matrix (no idea why Tikz does not accept \b13 as a variable) \qct is a text version of \qc: if nonnegative we put a \D = \phantom{-}.

Less complicated would be to define a command to compute \qct directly as an entry of a matrix of nodes.

Thanks

JOM
  • 457
  • 1
    TeX (not TikZ) doesn't support numbers in macro names (at least not without some nontrivial actions). – percusse Jul 01 '16 at 09:07