tikz - 并排放置两个节点

tikz - 并排放置两个节点

我的序言:

\documentclass{beamer}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning}

我想使用 tikz 将单词“event”和单词“cumulative”并排写出来。我的问题是单词“event”似乎略高于“cumulative”... 你能帮我吗?

\begin{document}
\begin{frame}

\begin{center}
\begin{tikzpicture}
\node (1st) {event};
\node[right=of 1st] (2nd) {cumulative};
\end{tikzpicture}
\end{center}

\end{frame}
\end{document}

在此处输入图片描述

答案1

当你使用right=ofTikZ 时,实际上将.west第二个节点的锚点放置在特定距离正确的第一个节点的.east锚点。对于具有不同高度和深度的节点,这些锚点(连同.center锚点)与基线的距离并不相同(锚点.text.base.base west.base east)。

使用base right=of锚点时,将使用.base west(新节点)和(引用节点)。这同样适用于锚点,因为锚点仅位于基线上方。.base eastmidmid rightmid.5ex

您还可以使用键和更改节点文本框的高度和深度text heighttext depth这会产生不同的效果,但如果您想绘制节点或将其用作垂直参考,则可能更合适。base right=of当然,您仍然可以在这里使用。除了猜测或设置特定的文本高度/深度外,您还可以使用\vphantom{<heighest and deepest characters>}(作为font键)或text height=\heightof{<heighest character>}等等。

在下面的例子中我使用了一个夸张的例子。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\tikzset{nodes={draw=gray, text=gray}}
\begin{document}
\begin{tikzpicture}
\node               (1st) {evena};
\node[right=of 1st] (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}

\begin{tikzpicture}
\node                    (1st) {evena};
\node[base right=of 1st] (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}

\begin{tikzpicture}
\node[text height=2em]  (1st) {evena};
\node[right=of 1st]     (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容