Tikz 在节点中对齐文本

Tikz 在节点中对齐文本

我正在制作日历的 Tikz 图片。日历的标题是月份,我用节点添加它们。但它们没有对齐。

\begin{tikzpicture}[scale = 1.3]
\path (0,0)node[minimum height = 1cm]{sep}
  (1,0)node[minimum height = 1cm]{oct}
  (2,0)node[minimum height = 1cm]{nov}
  (3,0)node[minimum height = 1cm]{dec}
  (4,0)node[minimum height = 1cm]{jan}
  (5,0)node[minimum height = 1cm]{feb}
  (6,0)node[minimum height = 1cm]{mar}
  (7,0)node[minimum height = 1cm]{apr}
  (8,0)node[minimum height = 1cm]{mei};
\foreach \x in {-0.5,0.5,...,8.5}
  \draw (\x,-0.3)--(\x,-5);
  \draw [fill = green] (-0.5,-1)rectangle node{Studie}(1.5,-1.5);
  \draw [fill = green] (1.5,-1.7)rectangle node{Structuur}(3.5,-2.2);
  \draw [fill = green] (3.5,-2.4) rectangle node{Dimensionering}(6,-2.9);
  \draw [fill = green] (6,-3.1) rectangle node {Interface} (8,-3.6);
\end{tikzpicture}

如何强制文本集中在一行上?

答案1

你需要阅读以下段落使用锚点定位节点 [anchor=base]是可能的。我稍微压缩一下你的代码

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale = 1.3]

 \foreach \m [count=\mi from 0] in {sep,oct,nov,dec,jan,feb,mar,apr,mei}
 \path[anchor=base] (\mi,0) node {\m} ;

\foreach \x in {-0.5,0.5,...,8.5}  \draw (\x,-0.3)--(\x,-5);

      \draw [fill = green]
        (-0.5,-1)   rectangle node {Studie}         (1.5,-1.5)
        (1.5,-1.7)  rectangle node {Structuur}      (3.5,-2.2)
        (3.5,-2.4)  rectangle node {Dimensionering} (6,-2.9)
        (6,-3.1)    rectangle node {Interface}      (8,-3.6);

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

只需为这些节点指定base锚点,然后文本就会与其基线对齐:

示例输出

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale = 1.3]
\path[anchor=base] (0,0)node[minimum height = 1cm]{sep}
  (1,0)node[minimum height = 1cm]{oct}
  (2,0)node[minimum height = 1cm]{nov}
  (3,0)node[minimum height = 1cm]{dec}
  (4,0)node[minimum height = 1cm]{jan}
  (5,0)node[minimum height = 1cm]{feb}
  (6,0)node[minimum height = 1cm]{mar}
  (7,0)node[minimum height = 1cm]{apr}
  (8,0)node[minimum height = 1cm]{mei};
\foreach \x in {-0.5,0.5,...,8.5}
  \draw (\x,-0.3)--(\x,-5);
  \draw [fill = green] (-0.5,-1)rectangle node{Studie}(1.5,-1.5);
  \draw [fill = green] (1.5,-1.7)rectangle node{Structuur}(3.5,-2.2);
  \draw [fill = green] (3.5,-2.4) rectangle node{Dimensionering}(6,-2.9);
  \draw [fill = green] (6,-3.1) rectangle node {Interface} (8,-3.6);
\end{tikzpicture}
\end{document}

相关内容