0
\usepackage{tikz} 
\usepackage{xstring} 

\begin{document}
\newcommand{\nG}[3]{  \node[opacity = 0, text opacity = 1, minimum size = 10mm] at (#1, #2){#3}; }

\newcommand{\writeSep}[4]{
\foreach \x in {1,...,#3}
{       
    \nG{\x-1}{#2}{\StrChar{#4}{\x}}; 
}}

\begin{figure}[H]\centering\subfloat[test]{%
\begin{tikzpicture}[font=\sffamily, scale=0.50, every node/.style={scale=1.00}]

\wrsg{0}{7}{25}{1482449257683314302317851}; //coord, coord, len of number, number

\end{tikzpicture}}%
\end{figure}
\end{document}

I have a macro for printing long numbers as separate digits, but I would like to multiply them before printing. Something like this:

\nG{\x-1}{#2}{\StrChar{#4}{\x} * 2};  

but this prints "num * 2" and not the actual multiplication. How can I do that?

2
  • 2
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Aug 24, 2015 at 20:08
  • sry..edited, should be ok now.
    – Martin877
    Aug 24, 2015 at 22:09

2 Answers 2

0

\StrChar of package xstring is not expandable and cannot be used in calculations. But it provides an optional argument to store the result:

\StrChar{#4}{\x}[\mynum]

Then the number can be used in calculations, e.g.:

\pgfmathsetmacro\myresult{\mynum * 2}

Then, \myresult can be used in macro \nG.

In case of simple calculations with integers, \numexpr can also be used:

\the\numexpr\mynum * 2\relax

This is used in the following example. Also the third argument of \wrsg is replaced by the calculation of the length via \StrLen.

\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}

\begin{document}
\newcommand*{\nG}[3]{%
  \node[
    opacity = 0,
    text opacity = 1,
    minimum size = 10mm,
  ] at ({#1}, {#2}) {#3};%
}

\newcommand*{\wrsg}[3]{%
  \StrLen{#3}[\LenOfNumber]%
  \foreach \x in {1,...,\LenOfNumber}{%
    \StrChar{#3}{\x}[\MyDigit]%
    \nG{\x-1}{#2}{\the\numexpr\MyDigit*2\relax};
  }%
}

\begin{figure}
  \centering
  \begin{tikzpicture}[
    font=\sffamily,
    scale=0.5,
    every node/.style={scale=1},
  ]
    \wrsg{0}{7}{1482449257683314302317851}; //coord, coord, len of number, number
  \end{tikzpicture}%
\end{figure}
\end{document}

Result

1

I’m not sure if this is what you are after, but here is my try using expl3.

\documentclass{article}
\usepackage{tikz,xparse}
\ExplSyntaxOn

\NewDocumentCommand \nG { m m m }
 {
  \node[opacity=0, text~opacity=1, minimum~size=10mm, anchor=base] at (#1, #2) {#3};
 }

\NewDocumentCommand \writeSep { m m m }
 {
  \martin_writesep:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Npn \martin_writesep:nnn #1#2#3
 {
  \seq_set_split:Nnn \l_tmpa_seq { } { #3 }
  \int_step_variable:nnnNn { 1 } { 1 } { #2 } \l_martin_int_tl
   {
    \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
    \quark_if_no_value:NF \l_tmpa_tl
     {
      \nG { \fp_eval:n { \l_martin_int_tl - 1 } } { #1 } { \fp_eval:n { \l_tmpa_tl * 2 } }
     }
   }
 }

\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}[font=\sffamily, scale=0.50, every node/.style={scale=1.00}]
  \writeSep{7}{25}{1482449257683314302317851}; % coord, len of number, number
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .