120

I'm a recent convert to TikZ from xy-pic, and am still trying to get a hang for it. I see that one can add labels to the end of arrows and the like using \node. Is there a way to add an arbitrary piece of text at a given position in the text, regardless of whether there is a shape there, a line there, or nothing at all? Thanks very much for your insight.

3
  • What do you mean with an arbitrary position in the text? Do you mean at an arbitrary position in your drawing, specified using a coordinate system? Or do you mean at a certain place in your LaTeX document?
    – Jake
    Sep 23, 2011 at 0:09
  • Sorry, I mean at an arbitrary position within the drawing itself, preferably specified by coordinates. Many thanks.
    – Skeptic
    Sep 23, 2011 at 0:12
  • 2
    No problem. Yeah, then using the \node at (<x>,<y>) {<text>} syntax as in Gonzalo's answer is the way to go.
    – Jake
    Sep 23, 2011 at 0:13

1 Answer 1

173

In TikZ you can use nodes to place almost anything (in particular, text) in the position you want. In the following example I used the at construct to specify the exact position of the nodes using explicit coordinates:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node[draw] at (0,0) {some text};
\node[draw,align=left] at (3,0) {some text\\ spanning three lines\\ with manual line breaks};
\node[draw,text width=4cm] at (2,-2) {some text spanning three lines with automatic line breaks};
\end{tikzpicture}

\end{document}

I only used the draw option to make the nodes visible. Notice that in the second \node I used the align= key and then used the \\ command to enforce the line breaks at the desired positions. In the third \node, instead of specifying the line breaks "manually", I specified a width for the text and now the line breaking was made automatically.

enter image description here

9
  • 1
    This is great, many thanks. For some reason, though, I get an error in trying to compile across the line containing "align=left". Any ideas on why this is? If I remove that, the picture compiles fine, although the line breaks are ignored. Hmm...
    – Skeptic
    Sep 23, 2011 at 0:17
  • 4
    Is it possible to somehow calculate the coordinates of a point on the edge of the boxes, maybe in percents or some other relative units, a point like the center of the "right border" of some box? I want to create arrows with it from one box to the next. Apr 16, 2017 at 22:24
  • 21
    +1. If I do not want to add the box around the text, I can use "\node[] at (0,0) {some text};" instead. Yes? Thanks
    – wonderich
    Nov 23, 2018 at 18:55
  • 7
    @user1700890 Just change \node[draw to \node[ (i.e. remove the draw). See explanation here.
    – Peter K.
    Mar 26, 2020 at 15:30
  • 2
    I just discovered that if you do not add align=left as the \draw parameter, the linebreak `\\` will not work. Sep 5, 2020 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .