![使用 tikz(日历),我如何控制节点位置?](https://linux22.com/image/289384/%E4%BD%BF%E7%94%A8%20tikz%EF%BC%88%E6%97%A5%E5%8E%86%EF%BC%89%EF%BC%8C%E6%88%91%E5%A6%82%E4%BD%95%E6%8E%A7%E5%88%B6%E8%8A%82%E7%82%B9%E4%BD%8D%E7%BD%AE%EF%BC%9F.png)
这是我根据 TikZ 手册改编的代码。我想做一个农业日历,但我对节点的位置感到困惑。非常感谢您的帮助。谢谢。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calendar,shadings}
\usetikzlibrary{calc}
\def\termin#1#2{
\node [text width= 3.4cm] at
($(cal-#1)+(0em, 0em)$) {\textcolor{red}\tiny{#2}};
}
\begin{document}
\sffamily
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{tikzpicture}
[transform shape,
every day/.style={anchor=mid,font=\fontsize{6}{6}\selectfont}]
%\node{\normalsize 2012};
\foreach \month in
{1,2,...,12}
{
% Compute angle:
\mycount=\month
\advance\mycount by -1
\multiply\mycount by 30
\advance\mycount by -90
% The actual calendar
\calendar at (\the\mycount:6cm)
[
dates=2012-\month-01 to 2012-\month-last, name=cal,
]
if (day of month=1) {\tikzmonthcode}
if (equals = 2012-11-20) [red]
if (equals = 2012-03-10) [red]
if (all)
{
% Again, compute angle
\mycount=1
\advance\mycount by -\pgfcalendarcurrentday
\multiply\mycount by 11
\advance\mycount by 90
\pgftransformshift{\pgfpointpolar{\mycount}{1.4cm}}
};
}
\termin{2012-11-20}{\color{red}Soybean planting}
\termin{2012-03-10}{\color{red}Soybean harvesting}
\end{tikzpicture}
\end{document}
答案1
您可以向 中添加一个参数来termin
指定定位选项。下面我将left=2.5ex
定位应用于“大豆种植”,并将 定位right=1.5ex
应用于“大豆收获”。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calendar,shadings}
\usetikzlibrary{calc}
\newcommand{\termin}[3][]{%
\node [#1] at
($(cal-#2)+(0em, 0em)$) {\textcolor{red}{#3}};
}
\begin{document}
\sffamily
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\noindent
\begin{tikzpicture}
[transform shape, scale=0.85,
every day/.style={anchor=mid,font=\fontsize{6}{6}\selectfont}]
%\node{\normalsize 2012};
\foreach \month in
{1,2,...,12}
{
% Compute angle:
\mycount=\month
\advance\mycount by -1
\multiply\mycount by 30
\advance\mycount by -90
% The actual calendar
\calendar at (\the\mycount:6cm)
[
dates=2012-\month-01 to 2012-\month-last, name=cal,
]
if (day of month=1) {\tikzmonthcode}
if (equals = 2012-11-20) [red]
if (equals = 2012-03-10) [red]
if (all)
{
% Again, compute angle
\mycount=1
\advance\mycount by -\pgfcalendarcurrentday
\multiply\mycount by 11
\advance\mycount by 90
\pgftransformshift{\pgfpointpolar{\mycount}{1.4cm}}
};
}
\termin[left=2.5ex]{2012-11-20}{\color{red}Soybean planting}
\termin[right=1.5ex]{2012-03-10}{\color{red}Soybean harvesting}
\end{tikzpicture}
\end{document}
答案2
这是一个答案。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar,shadings,positioning}
\usetikzlibrary{calc}
\def\termin#1#2#3#4{%
\node [align=left,text=red,font=\tiny, #2 =0ex of cal-#1,anchor=#3] {#4};
}%
\begin{document}
\sffamily
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{tikzpicture}
[transform shape,
every day/.style={anchor=mid,font=\fontsize{6}{6}\selectfont}]
%\node{\normalsize 2012};
\foreach \month in
{1,2,...,12}
{
% Compute angle:
\mycount=\month
\advance\mycount by -1
\multiply\mycount by 30
\advance\mycount by -90
% The actual calendar
\calendar at (\the\mycount:6cm)
[
dates=2012-\month-01 to 2012-\month-last, name=cal,
]
if (day of month=1) {\tikzmonthcode}
if (equals = 2012-11-20) [red]
if (equals = 2012-03-10) [red]
if (all)
{
% Again, compute angle
\mycount=1
\advance\mycount by -\pgfcalendarcurrentday
\multiply\mycount by 11
\advance\mycount by 90
\pgftransformshift{\pgfpointpolar{\mycount}{1.4cm}}
};
}
\termin{2012-11-20}{left}{east}{Soybean planting}
\termin{2012-03-10}{right}{west}{Soybean harvesting}
\end{tikzpicture}
\end{document}