[Tex/LaTex] TikZ ER diagrams: really can’t underline keys

databasetext-decorationstikz-pgf

I'm creating an ER diagram with the TikZ ER library. The manual warns that although key attributes are conventionally underlined, it is hard to implement in TeX and the library uses italics instead. That's fine for me but my teachers probably won't accept the assignment with non-standard notations.

That's why I'm asking the TeX wizard community for a working solution 🙂 Questions that may be relevant are:

MWE from the manual:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{er}

\begin{document}
\begin{tikzpicture}
\node[entity] (sheep) at (0,0) {Sheep}
            child {node [key attribute] {name}};
\node[entity] (genome) at (2,0) {Genome};
\node[relationship] at (1,1.5) {has}
            edge (sheep)
            edge (genome);
\end{tikzpicture}
\end{document}

Best Answer

Assuming I understand the requirements correctly, it is possible to 'capture' the node contents in a box using execute at begin node and execute at end node and do something afterwards with it. This only possible with 'simple' node contents (i.e., basically just text).

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{er}
\newbox\underlinebox
\tikzset{
    key attribute/.append style={
        font=\rmfamily,
        execute at begin node={%
            \setbox\underlinebox=\hbox\bgroup
        },
        execute at end node={%
             \egroup\underline{\box\underlinebox}%
        }
    }
}
\begin{document}
\begin{tikzpicture}
\node[entity] (sheep) at (0,0) {Sheep}
            child {node [key attribute] {name}};
\node[entity] (genome) at (2,0) {Genome};
\node[relationship] at (1,1.5) {has}
            edge (sheep)
            edge (genome);
\end{tikzpicture}
\end{document}

enter image description here