2

I'm trying to draw a diagram in TikZ with 45-degree lines, and I want to label these lines in such a way that the text of the label is also angled at 45 degrees. However I'm also interested in how to set it to a general angle. Basic example:

\documentclass{report}
\usepackage{tikz}
\begin{document}
\draw[thick] (-3,3) -- (3,-3)
\end{document}

I want to create labels at either end of this line, which have been rotated -45 degrees from horizontal, and which lie above-right of the line. Can anybody help me, please?

1

I guess you are searching the option sloped:

Here an example:

\documentclass{article}
\usepackage{tikz}
\begin{document}


\begin{tikzpicture}
  \draw[thick] (-3,3) -- (3,-3) node[at end,sloped,anchor=west] {foobar};
\end{tikzpicture}
\end{document}

To specify the position of the label you can use the option pos:

\draw[thick] (-3,3) -- (3,-3) node[pos=0.7,sloped,anchor=west] {foobar};

You can also use the library decoration to add material to a path:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[label/.style={%
   postaction={ decorate,transform shape,
   decoration={ markings, mark=at position .7 with \node #1;}}}]
\draw[,label={[above]{$r=0$}}]  (0,2.5) to [out=0,in=220] (3.25,3.75); 
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, that's nearly what I need. Is there a way to alter at which point/node the label is fixed? (i.e., not just the start or end point of the line, but some point in the middle) – Stromael May 26 '13 at 11:40
  • Is there a way to apply the same command to a curve? E.g., {\draw[thick] (0,2.5) to [out=0,in=220] (3.25,3.75) node[pos=0.7, sloped, anchor=west] {$r=0$};} – Stromael May 26 '13 at 12:29
  • Is there any way of just fixing a node and rotating the label around it by a desired angle? It's just that I have several curves to label in a similar way, and I am unsure how to adapt your code to an example with more than one curve and label. – Stromael May 26 '13 at 13:31
  • @Stromael: You should ask a new question. – Marco Daniel May 26 '13 at 13:32
  • You're quite right, and in fact this has already been answered here (for anybody else who happens to need an answer). Thanks anyway. – Stromael May 26 '13 at 13:43

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.