6

I am trying to learn TikZ, having some experience with pstrick. I would like to add some labels to my figures and am used to the convenience of the pstricks macro \uput{label separation}[relative angle](point){label}. For example \uput{8pt}[100](1,0){$A$} will put the label $A$ at the point (1,0), but 8 points away in the standard 100-degree direction. So is there something equivalent in TiKZ? In particular I would like to:

  • specify the distance or separation between the point and the label, and
  • specify the angle accurately, instead of being bound to 0, 45, 90, 135, 180, etc...

Here's a small example.

    \documentclass[12pt]{standalone}

    \usepackage{tikz}
    \usetikzlibrary{arrows.meta, fit}
    \usetikzlibrary{calc,intersections,through,backgrounds}

    \begin{document}
    \begin{tikzpicture}[x=0.06\linewidth, y=0.06\linewidth]
      \coordinate [label=45:$A$] (A) at (6,6);
      \coordinate [label=135:$B$] (B) at (1,4);
      \coordinate [label=-45:$D$] (D) at (4,1);
      \coordinate [label=-120:$C$] (C) at (0,0);
      \draw [line width=0.8pt] (A) -- (B) -- (C) -- (D) -- cycle;
      \path [draw, name path=A--C, line width=0.8pt] (A) -- (C);
      \path [draw, name path=B--D, line width=0.8pt] (B) -- (D);
      \path [name intersections={of=A--C and B--D, by=P}];
      %\draw (P) node[anchor=-100]{$P$};
    \end{tikzpicture}
    \end{document}

I would like to put a label at the point named P. The line I commented out does the job just fine, but I find it counterintuitive since my label is a node and not a label. Also, how do I adjust the separation between the label and the point?

Edit: Per @cfr's answer below, I can achieve what I need by inserting the line of code

    \draw (P) node[shift={(80:12pt)}]{$P$};

So, to summarize: an equivalent to the pstrick macro \uput{label separation}[relative angle](point/position){label} would be \draw (point/position) node[shift=(relative angle:label separation)]{label} in TikZ.

5
  • see TikZ & PGF Manual, section 17.10 The Label and Pin Options, page 238 – Zarko Oct 30 '16 at 19:21
  • @Zarko: I was indeed reading that section, and maybe I'm being dense, but I can't seem to be able to apply those commands. Partly, because I feel that if I use node then TikZ will draw something (a circle, a square, ...) there. I'm going to include a small example. – Minh Oct 30 '16 at 19:38
  • Labels you can ad to coordinates too. An example, what you like to achieve would be very helpful, – Zarko Oct 30 '16 at 19:52
  • every label/.style = {label distance = <length>, inner sep = <length>} ? Labels are actually nodes. – Zarko Oct 30 '16 at 20:06
  • Nodes have to have a shape, but they don't have to be drawn. All labels are nodes. – cfr Oct 30 '16 at 20:22
4

You might find the quotes library convenient.

Here's one way of adding the label without using the quotes syntax. I wouldn't actually bother with intersections here but would just use the midpoint of the short diagonal.

\begin{tikzpicture}[x=0.06\linewidth, y=0.06\linewidth, line width=.8pt]
  \coordinate [label=45:$A$] (A) at (6,6);
  \coordinate [label=135:$B$] (B) at (1,4);
  \coordinate [label=-45:$D$] (D) at (4,1);
  \coordinate [label=-120:$C$] (C) at (0,0);
  \draw  (A) -- (B) -- (C) -- (D) -- cycle (A) -- (C) (B) -- (D) node [midway, above] {$P$};
\end{tikzpicture}

Using the quotes library enables us to use a more concise syntax. Combined with the edge operation, we can then construct a similar diagram using the following line of code.

\draw (0,0) coordinate ["-120:$C'$"] (C') -- +(4,1) coordinate ["-45:$D'$"] (D') -- +(6,6) coordinate ["45:$A'$"] (A') edge (C') -- +(1,4) coordinate ["135:$B'$"] (B') edge ["$P'$" right, pos=.35]  (D') -- cycle;

similar shapes - different syntax

"<specification>" adds a label (in the case of a coordinate or node) or a node (in the case of an edge). "<specification>"' 'swaps' the location of the label or node if auto placement is being used. In the case of a label, "<angle>:<text>" can be used to place the label node at a precise angle. right, above etc. are redefined within the specification to refer to the various 'standard' angles you might choose. Standard styles are available to set up custom defaults for format and placement.

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[x=0.06\linewidth, y=0.06\linewidth, line width=.8pt]
  \coordinate [label=45:$A$] (A) at (6,6);
  \coordinate [label=135:$B$] (B) at (1,4);
  \coordinate [label=-45:$D$] (D) at (4,1);
  \coordinate [label=-120:$C$] (C) at (0,0);
  \draw  (A) -- (B) -- (C) -- (D) -- cycle (A) -- (C) (B) -- (D) node [midway, above] {$P$};
  \begin{scope}[xshift=50mm]
    \draw (0,0) coordinate ["-120:$C'$"] (C') -- +(4,1) coordinate ["-45:$D'$"] (D') -- +(6,6) coordinate ["45:$A'$"] (A') edge (C') -- +(1,4) coordinate ["135:$B'$"] (B') edge ["$P'$" right, pos=.35]  (D') -- cycle;
  \end{scope}
\end{tikzpicture}
\end{document}

EDIT

As I understand it, when placing a label or pin etc., TikZ tries to be rather more clever than you may wish. If you request a non-standard angle, it 'snaps' the anchor of the node to the nearest compass point e.g. 30, 90, 110 etc. So, to enforce the angle, you need to enforce the anchor.

For example,

... coordinate [label={[anchor=-5]-185:$C$}, label={[anchor=15]-165:$C$}, label={-135:$C$}, label={[anchor=60]-120:$C$}] (C) ...

produces

multiple labels with non-standard anchoring

To place the P label, I would just create a shifted edge node. For example,

... edge node [midway, shift=(80:12pt)] {$P$}  ...

produces

shifted edge node

Complete code:

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=0.06\linewidth, y=0.06\linewidth, line width=.8pt]
  \draw (0,0) coordinate [label={[anchor=-5]-185:$C$}, label={[anchor=15]-165:$C$}, label={-135:$C$}, label={[anchor=60]-120:$C$}] (C) -- +(4,1) coordinate [label=-45:$D$] (D) -- +(6,6) coordinate [label=45:$A$] (A) edge (C) -- +(1,4) coordinate [label=135:$B$] (B) edge node [midway, shift=(80:12pt)] {$P$}  (D) -- cycle;
\end{tikzpicture}
\end{document}
3
  • 1
    thanks for the answer. I'm not sure I can handle your quote usage yet. Got to check the documentation. However, I can still see in your code, that though we declare C at -120 degrees, TikZ keeps putting it at -135 degrees. I agree with you that I shouldn't bother with intersection here, but I want to learn to do intersections in general. In my particular example, I want to set the label P at 80 degrees and get the label farther from the intersection, say 12 pt. How do I achieve that? – Minh Oct 31 '16 at 0:47
  • @Minh Please see edit above. – cfr Oct 31 '16 at 3:28
  • The shift did it. Thanks, that's what I was looking for. – Minh Oct 31 '16 at 6:27
3

See, if the following MWE can help you:

\documentclass[ tikz,
                border=3mm,
                12pt]{standalone}
\usetikzlibrary{intersections}

    \begin{document}
\begin{tikzpicture}[x=0.06\linewidth, y=0.06\linewidth,
    every label/.style = {label distance=3pt, inner sep=1pt},
     every path/.style = {draw, line width=0.8pt}
                    ]
\coordinate [label=45:$A$] (A) at (6,6);
\coordinate [label=135:$B$] (B) at (1,4);
\coordinate [label=-45:$D$] (D) at (4,1);
\coordinate [label=-120:$C$] (C) at (0,0);
\path (A) -- (B) -- (C) -- (D) -- cycle;
\path [name path=A--C] (A) -- (C);
\path [name path=B--D] (B) -- (D);
\path [name intersections={of=A--C and B--D,by={[label=above:$P$]P}}];
\end{tikzpicture}
    \end{document}

enter image description here

4
  • Your MWE is what I had tried at first, but, as a beginner with TikZ, I didn't want to use the labeling option as I constructed the intersection. Also, I don't like being constrained to above, etc... I would like to be able set up a specific angle. Notice how label $C$ is not at -120 degrees but at -135 degrees. (Same if I put 95 degrees.) Is that something that can be changed in TikZ? I like label distance = 3pt. How do you specify it for only one particular label? E.g. all labels can be 3pt away, but what if I want one label to be 12 pt away? Thanks for the help so far. – Minh Oct 31 '16 at 0:37
  • Well, all those details are described in mentioned in the TikZ manual :-). For local changes of label distance you can ad it to label as \coordinate [label={[label distance = 0pt] 45:$A$}] ... Instead above ... you can use angles as is in your MWE but resolution of angles seems to be rounded to 5 degrees. – Zarko Oct 31 '16 at 0:52
  • 1
    Still, \path [name intersections={of=A--C and B--D, by={[label=80:$P$]P} }]; puts the label at 45 degrees instead of 80 degrees. And I get an error if I do \path [name intersections={of=A--C and B--D, by={[label=80:$P$, label distance=12pt]P} }];. Is my syntax wrong? – Minh Oct 31 '16 at 0:58
  • sory, my information in previous comment was wrong. node distance cannot be changed locally. Instead it you should use yshift and xshift. anyway, your syntax is wrong and also doesn't work in this case (can not be nested in already nested option). in this case try the following: \path [name intersections={of=A--C and B--D,by={P}}]; \coordinate[label={[yshift=12pt]85:$P$}] (p) at (P); Maybe here would be better use pin: \path [name intersections={of=A--C and B--D,by={[pin=85:$P$]P}}]; – Zarko Oct 31 '16 at 1:15

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.