4

Is there a way to draw only selected borders of a node? The node shape I have in mind is rectangle. The \node[draw]... syntax draws all four borders of the node. But what if I want to draw only the top and bottom borders?

One solution I can think of is to "manually" draw them, as below. But this doesn't seem very elegant. I'm looking for a key similar to [draw={<left/right/top/bottom>}].

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node(s){node text};
    \draw(s.north west)--(s.north east) (s.south west)--(s.south east);
\end{tikzpicture}
\end{document}

enter image description here

  • 2
    There is an example on TeXample that looks exactly like your image. There is also a question on TeX.sx which asks for that. If I could just find it though … – Qrrbrbirlbel Aug 15 '13 at 12:13
  • There we go: Cut one side of a rectangle node in TikZ — Now, is that something you can work off, or do you want it more sophisticated? (One can think of an option that either joins the sides correctly (only one style possible) and doesn’t draw it side for side or separate shapes (like the TeXample one and see my comment on that)?) – Qrrbrbirlbel Aug 15 '13 at 12:21
  • @Qrrbrbirlbel: Excellent! Your answer to that question solves my problem. – Herr K. Aug 15 '13 at 12:35
4

If this approach does not suit, say so and I'll remove it. In this case, rather than using tikz, I create a macro (\tbline)that does the over/underline. The drawback to this approach is that tikz then puts the baseline of the node at the level of the bottom line, rather than at the baseline of the text.

In this MWE, I first show the manual approach that you performed, followed by the approach I provide. For more completeness, I also add \lrline and \tblrline macros for doing left/right and full boxing according to the same baseline as \tbline.

\documentclass[border=2pt]{article}
\usepackage{stackengine}
\def\linethickness{.5pt}
\newcommand\tbline[1]{%
  \stackunder{%
    \stackon{#1}{\rule{\widthof{~#1~}}{\linethickness}}%
  }{%
    {\rule{\widthof{~#1~}}{\linethickness}}%
  }%
}
\newsavebox\nodebox
\newcommand\lrline[1]{%
  \sbox\nodebox{%
    \stackunder{%
      \stackon{#1}{\phantom{\rule{\widthof{~#1~}}{\linethickness}}}%
    }{%
      {\phantom{\rule{\widthof{~#1~}}{\linethickness}}}%
    }%
  }%
  \rule[-\dp\nodebox]{\linethickness}{\ht\nodebox+\dp\nodebox}%
  \usebox{\nodebox}%
  \rule[-\dp\nodebox]{\linethickness}{\ht\nodebox+\dp\nodebox}%
}
\newcommand\tblrline[1]{%
  \sbox\nodebox{%
    \tbline{#1}%
  }%
  \rule[-\dp\nodebox]{\linethickness}{\ht\nodebox+\dp\nodebox}%
  \usebox{\nodebox}%
  \rule[-\dp\nodebox]{\linethickness}{\ht\nodebox+\dp\nodebox}%
}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node(s){node text};
    \draw(s.north west)--(s.north east) (s.south west)--(s.south east);
\end{tikzpicture}
\begin{tikzpicture}
    \node(s){\tbline{node text}};
\end{tikzpicture}
\begin{tikzpicture}
    \node(s){\lrline{node text}};
\end{tikzpicture}
\begin{tikzpicture}
    \node(s){\tblrline{node text}};
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Thanks! This one also works, although I still prefer the answer linked in my comment above, as it also allows for other formatting possibilities. – Herr K. Aug 15 '13 at 15:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.