从命令输入中打印数学打印值

从命令输入中打印数学打印值

我正在尝试创建一个包含 2 个输入的新命令:半径和弧度。我已让所有命令正常工作,但当我编译时,弧度值打印出来为 2*pi/4(例如),而不是实际的 2 pi 符号除以 4。有办法解决这个问题吗?以下是新命令的代码;#1 = 半径和 #2=弧度

\newcommand{\arcR}[2]{
\begin{tikzpicture}[thick, font=\sffamily\Large,scale=0.5]
    \draw (0,0) circle[radius=5];
    \draw[very thick,fill=blue!30] 
        (0,0) -- ({deg(#2)}:5) 
        arc[start angle={deg(#2)}, end angle=0, radius=5] -- cycle;
    \draw[latex-latex] 
        ({deg(#2)}:1.2) 
        arc[start angle={deg(#2)}, end angle=0, radius=1.2] 
        node[above=5pt, midway] {$#2$};
    \draw[very thick,-latex] (0,0) -- (0:5) 
        node[midway,below=2pt]{$#1$};
\end{tikzpicture}
}

\arcR{5}{2*pi/3}

将产生以下图像:

在此处输入图片描述

答案1

考虑@Qrrbrbirlbel 评论和库anglesquotes

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles, arrows.meta,
                quotes}
\ExplSyntaxOn
\DeclareDocumentCommand{\symbreplace}{m}{
  \tl_set:Nx \l_tmpa_tl { #1 }
  \regex_replace_case_all:nN {
    { pi  }{ \c{pi}  }
    { *   }{         }
  } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\newcommand\arcRQ[2]{
\draw   (0,0) circle[radius=5];
\path[draw=blue, fill=blue!50, very thick, semitransparent]
        (0,0) coordinate (c) -- (0:5) coordinate (a)
                             arc (0:deg(#1):#2cm) coordinate (b)
                             -- cycle;
\pic [ANG, "$\symbreplace{#1}$"]  {angle = a--c--b};
              }

\begin{document}
    \begin{tikzpicture}[
         > = Straight Barb,
ANG/.style = {draw, semithick, <->,
              angle radius = 12mm,
              angle eccentricity=1.3,
              font=\Large\sffamily}
                    ]
%%%%
\arcRQ{2*pi/3}{5}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用包来解析数字l3regex,也许(但我不确定这是否是最佳解决方案,因为这仅适用于特定语法,如以下代码中的注释所述):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\ExplSyntaxOn
% transforms any number of the form x, x*pi, pi/x or x*pi/y 
% into properly styled math-mode
% where x and y can be any number, with or without decimal separator (dot)
\seq_new:N \l_topifrac_temp_seq
\NewDocumentCommand{\topifrac}{ m }{
    \regex_extract_once:nnN { \A(\d*\.?\d+)?(\*?pi)?(/(\d*\.?\d+))?\Z } 
        { #1 } \l_topifrac_temp_seq
    \seq_item:Nn \l_topifrac_temp_seq { 2 }
    \tl_if_blank:eTF { \seq_item:Nn \l_topifrac_temp_seq { 5 } } 
        { \tl_if_blank:eTF { \seq_item:Nn \l_topifrac_temp_seq { 3 } }  
            { }
            { \pi }
        }
        { \frac{\pi}{ \seq_item:Nn \l_topifrac_temp_seq { 5 } } }
}
\ExplSyntaxOff

\newcommand{\arcR}[2]{
    \begin{tikzpicture}[thick, font=\sffamily\Large, scale=0.5]
        \draw (0,0) circle[radius=5];
        \draw[very thick, fill=blue!30] 
            (0,0) -- ({deg(#2)}:5) 
            arc[start angle={deg(#2)}, end angle=0, radius=5] -- cycle;
        \draw[latex-latex] 
            ({deg(#2)}:1.2) 
            arc[start angle={deg(#2)}, end angle=0, radius=1.2] 
            node[above=5pt, midway] {$\topifrac{#2}$};
        \draw[very thick, -latex] (0,0) -- (0:5) 
            node[midway, below=2pt]{$#1$};
    \end{tikzpicture}
}

\begin{document}

\arcR{5}{2*pi/3}

\end{document}

在此处输入图片描述

答案3

一种方法是使用\arcR{5}{2\pi/3}输入,然后当您需要进行计算时,您可以用\pi替换*pi

在此处输入图片描述

代码:

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usepackage{xstring}

\newcommand{\arcR}[2]{
\begin{tikzpicture}[thick, font=\sffamily\Large,scale=0.5]
    \StrSubstitute{#2}{\pi}{*pi}[\RadianMeasure]% Replace '\pi' with '*pi'
    \pgfmathsetmacro\DegreeMeasure{deg(\RadianMeasure)}%
    %% -------------
    \draw (0,0) circle[radius=5];
    \draw[very thick,fill=blue!30] 
        (0,0) -- ({\DegreeMeasure}:5) 
        arc[start angle={\DegreeMeasure}, end angle=0, radius=5] -- cycle;
    \draw[latex-latex] 
        ({\DegreeMeasure}:1.2) 
        arc[start angle={\DegreeMeasure}, end angle=0, radius=1.2] 
        node[above=5pt, midway] {$#2$};
    \draw[very thick,-latex] (0,0) -- (0:5) 
        node[midway,below=2pt]{$#1$};
\end{tikzpicture}
}

\begin{document}
  \arcR{5}{2\pi/3}
\end{document}

相关内容