如何垂直对齐以下时间轴中的标签,以便看起来它们都位于基线上(因此垂直对齐在底部)?例如,\tau
和\tau+1
尚未垂直对齐在底部。因此我想使用位置,但 y 方向的对齐应该是可变的(目前\x
不是固定的)-2mm
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[thick,x=0.5cm]
\newcommand*{\mylabshift}{0.25}
\draw[->] (-1,0) -- (27,0) node[below=2mm]{Time};
\foreach \x in {0,1, 4,5, 9,10, 14, 19,20, 25}
\draw(\x, 1.2mm) -- (\x, -1.2mm);
\foreach \x/\what in {0/0, 1/1, 2.5/\dots, 4/\tau, 5+\mylabshift/\tau+1, 7.25/\dots, 9/t, 10+\mylabshift/t+1, 12.25/\dots, 14/t+h, 19-3*\mylabshift/T-h, 20+5*\mylabshift/T-h+1, 25/T}
\node[below] at (\x,-2mm) {$\what$};
\end{tikzpicture}
\end{document}
答案1
更新
定义timeline
环境并用于\event<*>[* or num or dimen]{num or dimen}{text}
添加事件。
- 首先
*
将用 来换行$...$
。 - 可选参数是刻度的 x 坐标,用于
*
设置无刻度。默认情况下,它与下一个参数相同。 - 第一个强制参数是事件节点的 x 坐标。
- 第二个强制性参数是文本。
\documentclass[tikz, margin=1cm]{standalone}
\makeatletter
\newtoks\t@event@toka%
\newtoks\t@event@tokb%
\newif\ifevent@math%
\newif\ifevent@comp%
\newenvironment{timeline}[1][0.25]{%
\begingroup%
\let\event@tmpa\pgfutil@empty%
\let\event@tmpb\pgfutil@empty%
\let\event@tmpc\pgfutil@empty%
\let\event@tmpd\pgfutil@empty%
\edef\event@min{\the\maxdimen}%
\edef\event@max{-\event@min}%
\def\event{%
\pgfutil@ifnextchar-%
{\event@compfalse\event@}%
{\event@comptrue\event@-}%
}%
\def\event@-{%
\pgfutil@ifnextchar*%
{\event@mathtrue\event@@}%
{\event@mathfalse\event@@*}%
}%
\def\event@@*{%
\pgfutil@ifnextchar[\event@@@{\@dblarg{\event@@@}}%
}%
\def\event@@@[##1]##2##3{%
\edef\event@tmpb{%
\noexpand\node[anchor=base] at (##2, -7mm)%
{\ifevent@math$##3$\else##3\fi};%
\ifx##1*\else%
\noexpand\draw (##1, -1.2mm) -- (##1, 1.2mm);%
\fi%
}%
\t@event@toka=\expandafter{\event@tmpa}%
\t@event@tokb=\expandafter{\event@tmpb}%
\edef\event@tmpa{\the\t@event@toka\the\t@event@tokb}%
\ifx##1*\else%
\event@set@axismax{##1}%
\fi%
\event@set@axismax{##2}%
}%
\def\event@set@axismax##1{%
\tikz@checkunit{##1}%
\iftikz@isdimension%
\pgf@process{\pgfpoint{##1}{0pt}}%
\else%
\pgf@process{\pgfpointxy{##1}{0}}%
\fi%
\pgfmathparse{\the\pgf@x}%
\let\event@cur\pgfmathresult%
\pgfmathsetmacro\event@min{%
\event@cur < \event@min ? \event@cur : \event@min}%
\pgfmathsetmacro\event@max{%
\event@cur > \event@max ? \event@cur : \event@max}%
}%
}{%
\t@event@toka=\expandafter{\event@tmpa\\}%
\edef\event@tmpa{%
\noexpand\matrix (a) [row sep=5pt] {%
\noexpand \draw[->] (\event@min pt - 1cm, 0) -- (\event@max pt + 1cm, 0);%
\the\t@event@toka%
};%
}%
\event@tmpa%
\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}[thick,x=0.5cm]
\newcommand*{\mylabshift}{0.25}
\begin{timeline}
\event*{0}{0}
\event*{1}{1}
\event*[*]{2.5}{\cdots}
\event*{4}{\tau}
\event*[5]{5+\mylabshift}{\tau+1}
\event*[*]{7.25}{\cdots}
\event*{9}{t}
\event*[10]{10+\mylabshift}{t+1}
\event*[*]{12.25}{\cdots}
\event*{14}{t+h}
\event*[19]{19-3*\mylabshift}{T-h}
\event*[20]{20+5*\mylabshift}{T-h+1}
\event*{25}{T}
\end{timeline}
\end{tikzpicture}
\end{document}
原始答案
\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick,x=0.5cm]
\newcommand*{\mylabshift}{0.25}
\draw[->] (-1,0) -- (27,0) node[anchor=base, yshift=-7mm]{Time};
\foreach \x in {0,1, 4,5, 9,10, 14, 19,20, 25}
\draw(\x, 1.2mm) -- (\x, -1.2mm);
\foreach \x/\what in {
0/0, 1/1, 2.5/\dots, 4/\tau, 5+\mylabshift/\tau+1, 7.25/\dots, 9/t,
10+\mylabshift/t+1, 12.25/\dots, 14/t+h, 19-3*\mylabshift/T-h,
20+5*\mylabshift/T-h+1, 25/T
}
\node[anchor=base] at (\x,-7mm) {$\what$};
\end{tikzpicture}
\end{document}