如何乘以字符串?

如何乘以字符串?
\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}

我有一个宏,用于将长数字打印为单独的数字,但我想在打印之前将它们相乘。像这样:

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

但这会打印“num * 2”,而不是实际的乘法。我该怎么做?

答案1

\StrCharof 包xstring不可扩展,不能用于计算。但它提供了一个可选参数来存储结果:

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

然后该数字可用于计算,例如:

\pgfmathsetmacro\myresult{\mynum * 2}

然后,\myresult即可在宏中使用\nG

如果使用整数进行简单计算,\numexpr也可以使用:

\the\numexpr\mynum * 2\relax

这在以下示例中使用。此外,的第三个参数也\wrsg被通过的长度计算所取代\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}

结果

答案2

我不确定这是否是您想要的,但这是我尝试使用的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}

在此处输入图片描述

相关内容