Tikz 中的条件格式以及如何使用变量进行数学运算

Tikz 中的条件格式以及如何使用变量进行数学运算

在此处输入图片描述这是我\ifthenelsexifthen包中生成的一个简单数字线的 MWE。

\documentclass{article}
\usepackage{tikz}
\usepackage{amsfonts}
\usepackage{xifthen}

\begin{document}

\begin{tikzpicture}

  \draw[->] (-5.5,0) -- (5.5,0) node [below] {$\mathbb{R}$};

  \foreach \x in {-5,...,5}
    \ifthenelse{\x = 2 \OR \x=4}{\draw (\x,0.1) -- (\x,-0.1) node [below,red] {\x}}{\draw (\x,0.1) -- (\x,-0.1) node [below,black] {\x}};

\end{tikzpicture}

\end{document} 

我怎样才能做一些简单的事情,比如用一个简单的公式将所有偶数变为红色?或者将所有 5 的倍数放大并加粗/蓝色。Tikz 可以做这样的事情吗?

答案1

比其他答案更为冗长,但允许将几乎每一点格式与绘图代码分离,并且使用键node contents允许对数字进行不同的排版。

\documentclass[tikz,border=5]{standalone}
\usepackage{amsfonts}
\newcount\tikzcount
\tikzset{%
  number/.code={%
    \tikzset{every number/.try=#1}%
    \ifodd#1\relax
      \tikzset{every odd number/.try=#1}%
    \else
      \tikzset{every even number/.try=#1}%
    \fi%
    \tikzset{execute for this number/.try=#1, number #1/.try=#1}%
  },
  execute for this number/.code={},
  number divisible by/.style args={#1 then #2}{%
    execute for this number/.append code={%
      \tikzcount=##1
      \divide\tikzcount by #1
      \multiply\tikzcount by #1
      \ifnum\tikzcount=##1 \tikzset{#2}\fi%
    }%
  }  
}
\begin{document}
\begin{tikzpicture}[%
  every number/.style={below=.333em, node contents=#1},
  every odd number/.style={red},
  every even number/.style={blue, node contents=$#1$},
  number divisible by=5 then {purple},
  number divisible by=3 then {font=\bfseries, blue},
  number 0/.style={shape=circle, fill=red!20, text=black},
  number -4/.style={xslant=0.25, fill=blue!20}
]
\draw[->] (-5.5,0) -- (5.5,0) node [below] {$\mathbb{R}$};
\foreach \x in {-5,...,5}
  \draw (\x,0.1) -- (\x,-0.1) (\x,0) node [number=\x];
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一种选择是使用evaluate <variable> as <macro> using <formula>。一个简单的例子:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsfonts}

\begin{document}

\begin{tikzpicture}
\draw[->] (-5.5,0) -- (5.5,0) node [below] {$\mathbb{R}$};
\foreach \x [evaluate=\x as \xeval using int(2*\x),evaluate=\x as \xevalo using int(2*\x+1)] in {-2,...,2}
{
   \draw (\xeval,0.1) -- (\xeval,-0.1) node [below,red] {$\xeval$};
   \draw (\xevalo,0.1) -- (\xevalo,-0.1) node [below,blue,font=\Huge] {$\xevalo$};
}
\end{tikzpicture}

\end{document} 

在此处输入图片描述

或者类似

\documentclass{article}
\usepackage{tikz}
\usepackage{amsfonts}

\begin{document}

\begin{tikzpicture}
\draw[->] (-5.5,0) -- (5.5,0) node [below] {$\mathbb{R}$};
\foreach \x  in {-5,...,5}
{ 
  \pgfmathparse{int(mod(\x,5))} 
  \ifnum0=\pgfmathresult\relax
     \draw (\x,0.1) -- (\x,-0.1) node [below,blue,font=\Huge] {$\x$};
   \else  
     \draw (\x,0.1) -- (\x,-0.1) node [font=\footnotesize,below,fill=red!20,circle,minimum size=2em] {$\x$};
  \fi
}
\end{tikzpicture}

\end{document} 

在此处输入图片描述

答案3

您还可以将颜色设置为循环内计算的一部分:

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

\begin{document}

\begin{tikzpicture}[big blue/.style={font=\bfseries\Large, blue}]

  \draw[->] (-5.5,0) -- (5.5,0) node [below] {$\mathbb{R}$};

  \foreach \x in {-5,...,5}
  {
    \pgfmathsetmacro\ncol{iseven(\x) ? "red": "black"}
    \draw (\x,0.1) -- (\x,-0.1) node [below,\ncol] {\x};
    \pgfmathsetmacro\ncol{( mod(\x,5) == 0 ) ? "big blue": "black"}
    \draw (\x,-1.1) -- (\x,-1.1) node [below,\ncol] {\x};
  }

\end{tikzpicture}

\end{document}

循环计算

相关内容