如何更改时间线上的行号?

如何更改时间线上的行号?

我有以下代码:

\begin{tikzpicture}
  \draw[line width=1pt] (0,0) -- (12,0)node[right=4mm]{(periods)};
  \foreach \x/\y in {0/0,4/1,8/2,12/3}{
    \draw[line width=1pt] (\x,-2mm)node[below](\x){\strut\y} -- (\x,2mm)node[above]{$\$ 18,000$};
    }
\end{tikzpicture}

这将产生:

在此处输入图片描述

但我想将 $18000 更改为不同的数字,例如 0 上的 $18000、1 上的 $19000、2 上的 $20000、3 上的 $21000。

我该如何改变当前代码来实现这一点?

答案1

在此处输入图片描述

所需要的只是对代码片段进行少量修改,即在\foreach循环中写入所需的数字:

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{siunitx}

\begin{document}
\begin{tikzpicture}
  \draw[line width=1pt] (0,0) -- (12,0)node[right=4mm]{(periods)};
  \foreach \x/\y in {18000/0,19000/1,20000/2,21000/3}%
{
\draw[line width=1pt] (4*\y,-2mm) node[below] {\y} -- ++(0,4mm) node[above] {\$ \num{\x}};
}
\end{tikzpicture}
\end{document}

或者使用以下稍微改进的图像代码:

\begin{tikzpicture}
  \draw[line width=1pt] (0,0) -- (12,0)node[right=4mm]{(periods)};
  \foreach \x [count=\z from 0] in {18000,19000,20000,21000}%
{
\draw[line width=1pt] (4*\z,-2mm) node[below] {\y} -- ++(0,4mm) node[above] {\SI{\x}[\$]{}};
}

这使:

在此处输入图片描述

答案2

这里没有必要迭代两个变量。只需迭代一个就足够了,要增加美元金额,只需使用评估选项计算千美元的数量即可:

[evaluate=\x as \dollars using int(18+\x)]

截屏

\documentclass[tikz,border=5mm]{standalone}
\usepackage{icomma}% <- Delete the space after a comma in a number in mathematical mode
\begin{document}   

\begin{tikzpicture}
  \draw[line width=1pt] (0,0) -- (12,0)node[right=4mm]{(periods)};
  \foreach \x [evaluate=\x as \dollars using int(18+\x)]in {0,...,3}{
    \draw[line width=1pt] (\x*4,-2mm)node[below](\x){\x} -- (\x*4,2mm)node[above]{$\$ \dollars,000$};
    }
\end{tikzpicture}
\end{document}

相关内容