tikz: set appropriate x value for a node
Asked Answered
C

2

5

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.

Comanchean answered 5/5, 2010 at 22:21 Comment(0)
A
6

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}
Albite answered 6/5, 2010 at 14:42 Comment(3)
Actually, I don't think you need calc. You can use the same line as presented by basweber, but just put (n3) instead of (n2). The key in this answer is defining the turning point of the rectilinear path (n3). Good call!Arsyvarsy
I tried using the original code (with "midpoint") but for some reason that ended up putting the node at the bottom of the brace (at the same y-coordinate as n2). I am not sure why that was the case.Albite
Thank you very much. It works and its a clean solution. @Geoff: You are right: ($(n1)!0.5!(n3)$) (and hence calc) is not needed. On my machine the two versions produce the same result. Thanks for pointing that out.Comanchean
H
1

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.

Hardcastle answered 6/2, 2017 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.