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

  • \multiply\aa by \bb is wrong if \aa is no count register. – user31729 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. – user31729 Jun 10 '16 at 16:03
  • See my answer please – user31729 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
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

  • Alternatively, use \let\aa\mynumberone etc. – user31729 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!! – user31729 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
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} 
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.