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 drawing, regardless of whether there is a shape there, a line there, or nothing at all? Thanks very much for your insight.
Asked
Active
Viewed 2.9e+01k times
121
Robert Pollak
- 117
Skeptic
- 3,025
- 5
- 26
- 23
1 Answers
174
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.

Gonzalo Medina
- 505,128
-
1This 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 '11 at 00:17
-
@Skeptic: Seems that you are using an outdated version of PGF. Add
\pgfversionto your code, and after compilation, you will see in your document the PGF version that your system is using. If your version number is less than2.10then you should update it. – Gonzalo Medina Sep 23 '11 at 00:21 -
4Is 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.
– Gal Grünfeld Apr 16 '17 at 22:24 -
+1 to @GalGrünfeld for the text placement question. Hopefully an answer will magically appear. – Bryan H-M Feb 15 '18 at 20:02
-
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 '18 at 18:55
-
-
7@user1700890 Just change
\node[drawto\node[(i.e. remove thedraw). See explanation here. – Peter K. Mar 26 '20 at 15:30 -
I added a plot and the coordinates do not line up. Any suggestions to get them to line up, so I can put a node with text at a point directle on the graph? – E2R0NS May 16 '20 at 13:23
-
2I just discovered that if you do not add
align=leftas the\drawparameter, the linebreak\\\will not work. – Eerik Sven Puudist Sep 05 '20 at 21:01
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 '11 at 00:09\node at (<x>,<y>) {<text>}syntax as in Gonzalo's answer is the way to go. – Jake Sep 23 '11 at 00:13