2

In the example below, I've managed to align the words on the baseline and the nodes above them correctly by setting text height = \Aheight for the words (i.e. let the height of all words be equal to the character A) and text depth = 0pt for the nodes (i.e. let the center of all nodes be the same). What I don't know how to do is to adjust the vertical space between the words and the nodes above. I want to raise the nodes above to a more appropriate place.

\documentclass{article}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\newcommand{\phraselabel}[2]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight, draw},
        above/.style = {inner sep = 0pt, text depth = 0pt, draw}%
        ]
    \node[txt] (word) {#1};
    \node[above] at (word.north) {\footnotesize{#2}};
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{gg}{xxxxx} \phraselabel{aa}{jjjjj} \phraselabel{tt}{xxxxx} \phraselabel{bb}{xxxxx}.
\end{document}

enter image description here

  • Any 'more appropriate place' in particular? Judgements of appropriateness tend to be in the eye of the beholder - especially since we have no idea what you're using this for. – cfr Apr 10 '15 at 20:50
  • @cfr It's hard to say in advance. Certainly the absolute minimum distance would be so that the characters in the words and the nodes don't collide, but I'd prefer some flexibility so that I can adjust the distance as needed. (This is only under the assumption that such adjustment isn't too complicated) – Sverre Apr 10 '15 at 20:53
  • 1
    If you use the positioning library, you can say e.g. \node [above=2pt of some node] {whatever}.... – cfr Apr 10 '15 at 21:12
2

You can specify a value for above:

\documentclass{article}
\usepackage{tikz}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}

\newcommand{\phraselabel}[2]{%
    \begin{tikzpicture}[%
        baseline = (word.base),%
        txt/.style = {inner sep = 0pt, text height = \Aheight},%
        tag/.style = {above=0.75ex, inner sep = 0pt, text depth = 0pt}% (tweak the above value as needed)
        ]%
    \node[txt] (word) {#1};%
    \node[tag] at (word.north) {\footnotesize{#2}};%
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{gg}{xxxxx} \phraselabel{aa}{jjjjj} \phraselabel{tt}{xxxxx} \phraselabel{bb}{xxxxx}.
\end{document}

enter image description here

1

It is a bad idea to overwrite TiKZ's default above because then you can't use it to specify a relative position for the node above the word! The positioning library enhances this but only if you use a new name for your style.

The following places the node a specified distance above the word. By default, this is \jdepth which is equal to the depth of the letter 'j' in footnote sized text. But you can override this using an optional first argument to \phraselabel.

Obviously, you wouldn't normally want to use different distances on the same line - the second 2 lines are just to illustrate the effect of adding \jdepth to the specified dimension.

\documentclass{article}
\usepackage{tikz,calc}
\usetikzlibrary{positioning}
\newlength{\Aheight}
\setlength{\Aheight}{\fontcharht\font`A}
\newlength{\jdepth}
\settodepth{\jdepth}{\footnotesize j}


\newcommand{\phraselabel}[3][\jdepth]{%
    \begin{tikzpicture}[%
        baseline = (word.base),
        txt/.style = {inner sep = 0pt, text height = \Aheight, draw},
        my above/.style = {inner sep = 0pt, text depth = 0pt, draw}%
        ]
    \node[txt] (word) {#2};
    \node [my above, above=#1 of word.north] {\footnotesize{#3}};
    \end{tikzpicture}%
    }

\begin{document}
\phraselabel{gg}{xxxxx} \phraselabel{aa}{jjjjj} \phraselabel{tt}{xxxxx} \phraselabel{bb}{xxxxx}.\bigskip

\phraselabel[5pt]{gg}{xxxxx} \phraselabel[\jdepth+5pt]{bb}{xxxxx}\bigskip

\phraselabel[-1pt]{aa}{jjjjj} \phraselabel[\jdepth-1pt]{tt}{xxxxx}

\end{document}

demonstration

I have to wonder, though, if you are reinventing the wheel. While I'm not clear what you are trying to do, I kind of doubt that this is the most effective or efficient way to do it.

  • I agree that this is reinventing the wheel a bit. I just posted a new suggestion using expex to get the words aligned with their tags and then tikz to connect them when necessary. – Jason Zentz Apr 10 '15 at 22:39
  • My goal is just to add some labels above words in a sentence (see original question). But I would like the labels to be horizontally aligned, and at a "proper" distance from the words. I'm very open to other ways of accomplishing that goal. – Sverre Apr 11 '15 at 13:23
  • @Sverre You should look at the glosses answer to your other question, I think. – cfr Apr 11 '15 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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