3

I'm trying to get a line produced by tikzmarknodes to start and end with little hooks, i.e., horizontal thinner lines protruding from the ends, more or less to resemble a bracket. I couldn't figure out how to manipulate the node code.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

This is some text and \tikzmarknode{a}this is some text.

This is some text and \tikzmarknode{b}this is some text.
\begin{tikzpicture}[remember picture]  \draw[overlay,semithick] (a.west) -- (b.west); \end{tikzpicture}

This is some text and \begin{tikzpicture}\draw[overlay,very thin] (0.05,0.1) -- (0,0.1); \draw[overlay,semithick] (0,0.1) -- (0,-0.35); \draw[overlay,very thin] (0,-0.35) -- (0.05,-0.35); \end{tikzpicture}~this is some text.

This is some text and ~this is some text.


\end{document}

The code of the two last lines produce more or less what I am envisioning:

enter image description here

Any help here from the tikz masters?

jan
  • 2,236

1 Answers1

5

One possible way.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

This is some text and \tikzmarknode{a}{~this} is some text.

This is some text and \tikzmarknode{b}{~this} is some text.
\begin{tikzpicture}[overlay,remember picture]  \draw[very thin] 
([xshift=0.2em]a.west) -- ([xshift=-0.3pt]a.west)
([xshift=0.2em]b.west) -- ([xshift=-0.3pt]b.west);
 \draw[semithick] (a.west) -- (b.west); \end{tikzpicture}

This is some text and \begin{tikzpicture}\draw[overlay,thin] (0.05,0.1) -- (0,0.1); \draw[overlay,semithick] (0,0.1) -- (0,-0.35); \draw[overlay,thin] (0,-0.35) -- (0.05,-0.35); \end{tikzpicture}~this is some text.

This is some text and ~this is some text.
\end{document}

enter image description here

ADDENDUM: For more general settings, you may want to work with styles and fill.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{brace me/.style n args={2}{insert path={([xshift=0.2em,yshift=0.1pt]#1.west) -- 
([xshift=-0.3pt,yshift=0.1pt]#1.west) -- 
([xshift=-0.3pt,yshift=-0.1pt]#2.west) -- 
([xshift=0.2em,yshift=-0.1pt]#2.west) -- 
([xshift=0.2em,yshift=0.1pt]#2.west) --
([xshift=0.3pt,yshift=0.1pt]#2.west) --
([xshift=0.3pt,yshift=-0.1pt]#1.west) --
([xshift=0.2em,yshift=-0.1pt]#1.west) --
 cycle}}}
\begin{document}

This is some text and \tikzmarknode{a}{~this} is some text.

This is some text and \tikzmarknode{b}{~this} is some text.
\begin{tikzpicture}[overlay,remember picture] 
 \fill[brace me={a}{b}] ;
\end{tikzpicture}

This is some text and \tikzmarknode{a'}{~this} is some text.

This is some text ~and \tikzmarknode{b'}{~this} is some text.
\begin{tikzpicture}[overlay,remember picture] 
 \fill[brace me={a'}{b'}] ;
\end{tikzpicture}
\end{document}

enter image description here

  • Almost ;) Is there a way to have the hooks very thin? – jan Apr 05 '19 at 17:51
  • @jan only the hooks or the full bracket? –  Apr 05 '19 at 17:53
  • Only the hooks, like in the code below the vertical line was semithick and the horizontal lines very thin – jan Apr 05 '19 at 17:54
  • @jan I added something. (If you want to make sure that the line is always vertical regardless of whether or not the nodes are precisely on top of each other, this also possible but requires more work and more input in the form of a prescription of what is to be done in that case.) –  Apr 05 '19 at 17:57
  • The hooks should always be horizontal, even if the line ends up not being. So that's ok. But one more thing: Now given the different thicknesses, they thin lines don't connect nicely at the ends (I had the same problem with the bad solution below.) Is there a way to get them to connect fully to the vertical line, i.e., the thing being treated as one drawing? I guess that might be a more general question: Can I change the thickness in a draw command on the go? – jan Apr 05 '19 at 18:02
  • @jan Good catch! to cure this, I made the thin lines longer by half of the line width of a semi-thick line. –  Apr 05 '19 at 18:05
  • That's great and for vertical lines it works. But in a few cases they might not end up being fully vertical. So there's no way to tell tikz do it in one single draw command where the thickness changes at a node? For example, if you move the b-node one word to the left before the "and" it looks pretty bad ... -- I have accepted it though, as you clearly did a great job answering the original question ;) – jan Apr 05 '19 at 18:11
  • @jan I added a version for that as well. –  Apr 05 '19 at 18:19
  • Although (sorry to keep bugging), when the line is very tilted, it gets thinner at the end (almost having a 3-D effect). Is that intended? – jan Apr 05 '19 at 18:28
  • @jan Well, it depends on what you want to do. You can add [rounded corners=...] at various places in the path to have a different effect. –  Apr 05 '19 at 18:32
  • guess I have to take some tikz lessons ;) ... I'll play around with it a bit, you've given me ample resources! Immensely helpful! – jan Apr 05 '19 at 18:50