通过编程简化数学表达式

通过编程简化数学表达式

如何简化表达,使其更自然

  • -2pi/2变成-pi

  • -1pi/2变成-pi/2

  • 0pi/2变成0

  • 1pi/2变成pi/2

  • 2pi/2变成pi

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw   (-pi,0) -- (pi,0);
    \draw \foreach \i in {-2,-1,0,...,2}{(\i*pi/2,-.1) node[below, font=\tiny] {$\i\pi/2$} -- (\i*pi/2,.1)};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我介绍\simplifyhalves{<index>}{<term>}将在数学模式下呈现的简化版本<index><term>/2

-1\pi已编辑以过滤掉as -\pi1\pias\pi0\pias的特殊情况0

\documentclass[tikz,border=5mm]{standalone}
\makeatletter
\let\stripPT\strip@pt
\makeatother
\newcommand\simplifyhalves[2]{%
  \def\j{\stripPT\dimexpr.5\dimexpr#1 pt\relax\relax}%
  \ifodd#1%
    $\i#2/2$%
  \else%
    \def\theterm{#2}%
    \ifnum\j=-1\relax\def\j{-}%
    \else%
      \ifnum\j=1\relax%
        \def\j{}%
      \else
        \ifnum\j=0\relax%
          \def\theterm{}%
        \fi%
      \fi%
    \fi%
    $\j\theterm$%
  \fi%
}
\begin{document}
\begin{tikzpicture}
    \draw   (-pi,0) -- (pi,0);
    \draw \foreach \i in {-2,-1,0,...,2}{(\i*pi/2,-.1) node[below, font=\tiny] {%
      \simplifyhalves{\i}{\pi}} -- (\i*pi/2,.1)};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw   (-pi,0) -- (pi,0);
    \foreach \i [count=\j from -2] in {-\pi,-\pi/2,0,\pi/2,\pi}
        \draw (\j*pi/2,.1) -- + (0,-0.2) node[below, font=\tiny] {$\i$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容