我今天了解到我可以在 TikZ 中执行循环,这让我的生活变得轻松很多......但我想知道如何在数学模式下评估数值表达式。
梅威瑟:
\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
%TikZ
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
%TikZ
\begin{document}
\begin{center}
\begin{tikzpicture}[>=stealth,ultra thick]
%arrows
\draw[->] (0,0) -- (11,0);
%curly braces
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39}
\draw[decorate,decoration={brace,amplitude=3pt,mirror}]
(\x,-1.25) coordinate (t_k_unten) -- (\x+2,-1.25) coordinate (t_k_opt_unten);
%vertical dotted lines
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39, 10/40}
\draw [dotted] (\x, -1.25) -- (\x, -0.5);
%nodes
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39}
%%%HERE IS THE LINE OF INTEREST:
\node at (\x+1,-1.75){$K_{35} = \n-35$};
\draw (-1,0) node[below] {Age};
%labels for nodes
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39, 10/40}
\draw (\x,0) node(\n)[below] {\n} -- (\x,0.5);
\end{tikzpicture}
\end{center}
\end{document}
我特别感兴趣的代码是
\node at (\x+1,-1.75){$K_{35} = \n-35$};
有什么方法可以\n-35
在数学模式下执行减法吗?例如,在数学模式下,输出应为 K_35 = 0、K_35 = 1 等。
编辑:我接受的解决方案在数学模式之外创建了一个变量,并在节点中使用了结果。这对于我的目的来说没问题。
答案1
这样的事情会起作用吗?
\documentclass[12pt,tikz,border=5pt]{standalone}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
[
>=stealth,
ultra thick,
]
\draw[->] (0,0) -- (11,0);
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39}
{
\draw[decorate,decoration={brace,amplitude=3pt,mirror}] (\x,-1.25) coordinate (t_k_unten) -- (\x+2,-1.25) coordinate (t_k_opt_unten);
%%%HERE IS THE LINE OF INTEREST:
\pgfmathsetmacro{\mysubtractresult}{int(\n-35)}
\node at (\x+1,-1.75){$K_{35} = \mysubtractresult$};
}
\foreach \x/\n in {0/35, 2/36, 4/37, 6/38, 8/39, 10/40}
{
\draw [dotted] (\x, -1.25) -- (\x, -0.5);
\draw (\x,0) node(\n)[below] {\n} -- (\x,0.5);
}
\draw (-1,0) node[below] {Age};
\end{tikzpicture}
\end{document}