如何简化表达,使其更自然
-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 -\pi
、1\pi
as\pi
和0\pi
as的特殊情况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}