tikz: set appropriate x value for a node

纵然是瞬间 提交于 2019-12-05 00:10:05

问题


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.


回答1:


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}



回答2:


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}

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.



来源:https://stackoverflow.com/questions/2777179/tikz-set-appropriate-x-value-for-a-node

标签

工具导航Map

JSON相关