tikz:为节点设置适当的x值

发布于 2024-08-31 07:13:52 字数 1184 浏览 7 评论 0原文

这个问题源于问题这里

我想生成一个跨越一些行的大括号的文本。 问题是我必须手动对齐 x 坐标,这不是一个干净的解决方案。

目前我使用

\begin{frame}{Example}

\begin{itemize}
\item The long Issue 1
\tikz[remember picture] \node[coordinate,yshift=0.7em] (n1) {}; \\
spanning 2 lines


\item Issue 2
  \tikz[remember picture] \node[coordinate, xshift=1.597cm] (n2) {};
\item Issue 3

\end{itemize}

\visible<2->{
\begin{tikzpicture}[overlay,remember picture]
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (n1) -- (n2) node[midway, right=4pt] {One and two are cool};
\end{tikzpicture}
 } % end visible

\end{frame}

它产生所需的结果:

tikz example1

令人不满意的是,我必须计算出 xshift 值1.597cm 通过反复试验(或多或少)

没有 xshift 参数,结果是:

tikz example 1

我猜有一个避免显式 xshift 值的优雅方法。

恕我直言,最好的方法是计算两个节点的最大 x 值并使用它(正如 Geoff 已经建议的那样) )

但是如果能够显式定义两个节点的绝对 x 值,同时保留它们当前的 y 值,那就已经非常方便了。这将避免调整小数点后第三位以确保大括号看起来垂直的繁琐过程。

This question resulted from the question here

I want to produce a curly brace which spans some lines of text.
The problem is that I have to align the x coordinate manually, which is not a clean solution.

Currently I use

\begin{frame}{Example}

\begin{itemize}
\item The long Issue 1
\tikz[remember picture] \node[coordinate,yshift=0.7em] (n1) {}; \\
spanning 2 lines


\item Issue 2
  \tikz[remember picture] \node[coordinate, xshift=1.597cm] (n2) {};
\item Issue 3

\end{itemize}

\visible<2->{
\begin{tikzpicture}[overlay,remember picture]
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (n1) -- (n2) node[midway, right=4pt] {One and two are cool};
\end{tikzpicture}
 } % end visible

\end{frame}

which produces the desired result:

tikz example1

The unsatisfying thing is, that I had to figure out the xshift value of 1.597cm by trial and error (more or less)

Without xshift argument the result is:

tikz example 1

I guess there is an elegant way to avoid the explicit xshift value.

The best way would it imho be to calculate the maximum x value of two nodes and use this, (as already suggested by Geoff)

But it would already be very handy to be able to explicitly define the absolute xvalues of both nodes while keeping their current y values. This would avoid the fiddly procedure of adapting the third post decimal position to ensure that the brace looks vertical.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2024-09-07 07:13:52

这需要 \usetikzlibrary{calc}。不过,可能有一种更清洁的方法。

从节点 n2 中删除“xshift”,然后使用:

\begin{tikzpicture}[overlay,remember picture]
  \path (n2) -| node[coordinate] (n3) {} (n1);
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (n1) -- (n3);
  \node[right=4pt] at ($(n1)!0.5!(n3)$) {One and two are cool};
\end{tikzpicture}

This requires \usetikzlibrary{calc}. There may be a cleaner way, though.

Remove the "xshift" from node n2 and then use:

\begin{tikzpicture}[overlay,remember picture]
  \path (n2) -| node[coordinate] (n3) {} (n1);
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (n1) -- (n3);
  \node[right=4pt] at ($(n1)!0.5!(n3)$) {One and two are cool};
\end{tikzpicture}
深陷 2024-09-07 07:13:52

这是一个使用 fit 库的版本,它不需要您担心哪一行最长,但需要标记每行。

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{fit}

\newcommand{\bracemark}[1]{\tikz[remember picture] \node[inner sep=0pt] (#1) {\vphantom{X}};}

\begin{document}
\begin{frame}{Example}

\begin{itemize}
\item The long Issue 1        \bracemark{n1} \\
gratuitious long line of text \bracemark{n2} \\
spanning 3 lines              \bracemark{n3}

\item Issue 2                 \bracemark{n4}
\item Issue 3

\end{itemize}

\visible<2->{
\begin{tikzpicture}[overlay,remember picture]
  \node [inner sep=0pt, fit=(n1) (n2) (n3) (n4)] (bracemarks) {};
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (bracemarks.north east) -- (bracemarks.south east) node[midway, right=4pt] {One and two are cool};
\end{tikzpicture}
 } % end visible

\end{frame}

\end{document}

图片输出

通过将节点设为实际节点(而不是坐标)并使用零宽度 X 作为文本,可以避免 OP 示例中所需的 yshift。

Here's a version using the fit library which doesn't require you to worry about which line is longest, at the expense of marking each line.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{fit}

\newcommand{\bracemark}[1]{\tikz[remember picture] \node[inner sep=0pt] (#1) {\vphantom{X}};}

\begin{document}
\begin{frame}{Example}

\begin{itemize}
\item The long Issue 1        \bracemark{n1} \\
gratuitious long line of text \bracemark{n2} \\
spanning 3 lines              \bracemark{n3}

\item Issue 2                 \bracemark{n4}
\item Issue 3

\end{itemize}

\visible<2->{
\begin{tikzpicture}[overlay,remember picture]
  \node [inner sep=0pt, fit=(n1) (n2) (n3) (n4)] (bracemarks) {};
  \draw[thick,decorate,decoration={brace,amplitude=5pt}]
        (bracemarks.north east) -- (bracemarks.south east) node[midway, right=4pt] {One and two are cool};
\end{tikzpicture}
 } % end visible

\end{frame}

\end{document}

Image of output

The yshift needed in the OP's sample is avoided by making the nodes actual nodes (as opposed to coordinates) with a zero-width X as text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文