5

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

Herr K.
  • 17,946
  • 4
  • 61
  • 118

1 Answers1

5

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

  • 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