我想将文本排列在由 TikZ 创建的日历。有没有办法使用相对放置(手册第 3.8 节)或类似的东西?
例如,我想将“文本 2”放在“文本 1”下方(并且仍在正确的日历行中)。
我的 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar}
\begin{document}
\begin{tikzpicture}[every day/.style={anchor=center}]
\calendar[dates=2000-01-01 to 2000-01-last,
week list]
if (equals=2000-01-03) {\draw (0,0) circle (7.5pt); \node [right=5cm] {Text 1};}
if (equals=2000-01-11) {\draw (0,0) circle (7.5pt); \node [right=5cm] {Text 2};}
if (equals=2000-01-16) {\draw (0,0) circle (7.5pt); \node [right=5cm] {Text 3};}
;
\end{tikzpicture}
\end{document}
电流输出:
答案1
可能有更好或其他方法可以做到这一点--我了解得calendar
不够多。
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calendar}
\begin{document}
\begin{tikzpicture}[every day/.style={anchor=center}]
\coordinate (col1) at (5,0);
\coordinate (col2) at (6.5,0);
\calendar[dates=2000-01-01 to 2000-01-last,
week list]
if (equals=2000-01-03) {\draw (0,0) circle (7.5pt); \node at (col1|-0,0) {Text 1};}
if (equals=2000-01-11) {\draw (0,0) circle (7.5pt); \node at (col1|-0,0) {Text 2};}
if (equals=2000-01-16) {\draw (0,0) circle (7.5pt); \node at (col2|-0,0) {Text 3};}
;
\end{tikzpicture}
\end{document}
编辑:第一个节点被命名,并且下一个节点相对于该节点放置。- 相同输出。
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calendar}
\begin{document}
\begin{tikzpicture}[every day/.style={anchor=center}]
\calendar[dates=2000-01-01 to 2000-01-last,
week list]
if (equals=2000-01-03) {\draw (0,0) circle (7.5pt); \node[right=5cm] (A) {Text 1};}
if (equals=2000-01-11) {\draw (0,0) circle (7.5pt); \node at (A|-0,0) {Text 2};}
if (equals=2000-01-16) {\draw (0,0) circle (7.5pt); \node at ([xshift=1.5cm]A|-0,0) {Text 3};}
;
\end{tikzpicture}
\end{document}
编辑:使用不同长度的文本并进行左侧调整:
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calendar}
\begin{document}
\begin{tikzpicture}[every node/.style={anchor=base west}, every day/.style={anchor=center}]
\calendar[dates=2000-01-01 to 2000-01-last,
week list]
if (equals=2000-01-03) {\draw (0,0) circle (7.5pt); \node[right=4cm] (A) {Text 1};}
if (equals=2000-01-11) {\draw (0,0) circle (7.5pt); \node at (A.west|-0,-0.1) {A very long Text 2};}
if (equals=2000-01-16) {\draw (0,0) circle (7.5pt); \node at ([xshift=3cm]A|-0,-0.1) {Text 3};}
;
\end{tikzpicture}
\end{document}
答案2
这个想法是当每周第一个节点被放置时切换布尔值。
如果有,我们将下一个标签相对于前一个节点放置(并将其命名为相同名称,以便可能的第三个标签放置在更靠右的位置)。
如果不是,我们将其放置在与当前星期几相关的位置(星期一 = 0,星期日 = 6)。
使用base
日期锚点可以更轻松地放置额外节点。不过,我们也可以只将第一个额外节点相对于日期节点的任何锚点进行放置。(不过,由于原点已经位于该锚点,因此在这种简单的日历样式中没有必要这样做。)
我们也可以检查节点的存在,但我们需要不存在周日之后(就像切换一样)或每周定义一次(通过全局计数器或周数本身),但如果您的文档中有多个日历,这些就会变得有趣(并且浪费?)。
切换似乎是最直接的。
代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar}
\newif\iftikzCalLabelPlacedThisWeek
\makeatletter
\newcommand*\tikzCalDayXshift{\tikz@lib@cal@xshift}
\pgfkeys{/utils/TeX/IF/.code n args={3}{\csname if#1\endcsname
\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi
{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}}
\makeatother
\tikzset{
% needs to be global because "if" will be executed inside group
% could also check for existence of a node on a week for week basis
% but that needs more overhead when multiple calendars will be typeset.
cal label node placed/.code={\global\tikzCalLabelPlacedThisWeektrue},
cal label node/.style={
name=cal-thisweek, % for reference for next cal label
anchor=base west,
/utils/TeX/IF={tikzCalLabelPlacedThisWeek}{
% we've already placed one cal label? place the next one next to it
xshift=1mm, at=(cal-thisweek.base east)
}{ % first cal label this week: off set weekday shifting
at={({(7-\pgfcalendarcurrentweekday)*\tikzCalDayXshift},0)}
% at={([xshift=(7-\pgfcalendarcurrentweekday)*\tikzCalDayXshift]\tikzlastnode.base)}
},
cal label node placed},
cal label/.style={
days={
append after command={
(\tikzlastnode.center) edge[to path={circle[radius=7.5pt]}, draw]()
node[cal label node]{#1}}}},
week list with cal labels/.style={
week list,
% initialize false in case we've had same cal in document before
/utils/exec=\tikzCalLabelPlacedThisWeekfalse,
% doesn't need to be global because we're not inside the day group
execute after day scope=\ifdate{Sunday}{\tikzCalLabelPlacedThisWeekfalse}{}}}
\begin{document}
\begin{tikzpicture}[every day/.style={anchor=base}]
\calendar[dates=2000-01-01 to 2000-01-last,
week list with cal labels]
if (equals=2000-01-03) [cal label=Text 1]
if (equals=2000-01-11) [cal label=A very long Text 2]
if (equals=2000-01-16) [cal label=Text 3]
;
\end{tikzpicture}
\end{document}