Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass{article}

\usepackage{lmodern}
\usepackage{tikz,times}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[scale=.3]
    \draw [thick] (0,6) rectangle (18,4) node [pos=.5] {like this};

    \draw [thick] (0,2) -- (18,2) -- (18,0) -- (0,0) -- cycle;
    %\draw [thick] (0,2) -- (18,2) -- (18,0) -- (0,0) -- cycle node [pos=.5] {A};

    \draw [thick] (0,-3) -- (4, -3) -- (5, -5) -- (-1, -5) -- (3, -4) -- cycle;
    %\draw [thick] (0,-3) -- (4, -3) -- (5, -5) -- (-1, -5) -- (3, -4) -- cycle node [pos=.5] {B};
\end{tikzpicture}
\end{document}

In this document i have three paths. the first is a simple rectangle where positioning a node in the center is easy (node [pos=.5] {like this}). However when it comes to doing this for any path i am unable to do it.

How do i get a node that is centered. Optionally, one that is also inside the path, though this would not be as important.

share|improve this question
    
Do you want the node appear in the center of a shape or half the way along the path? –  Tobi Feb 8 '13 at 14:24
    
In the center of a shape. –  Johannes Feb 8 '13 at 14:37
    
See my answer below. It seems possible to get the center of a rectangle surrounding all points but that isn’t necessarily the “center of mass” –  Tobi Feb 8 '13 at 14:38
add comment

1 Answer 1

up vote 9 down vote accepted

The pos option (or midway) referres alway to the last two given coordinates, i.e. in a path (0,0) -- (1,2) -- (3,-1) with the node at the end it will be centered on the line between (1,2) and (3,-1):

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) -- (1,2) -- (3,-1) node [pos=0.5, fill=red] {x};
\end{tikzpicture}
\end{document}

centered node

To center a node in a shape with more edges you may use a hint: Create the path and then let a node fit all it’s coordinates (using the fit library). The content of this node will be centered in a rectangle (add draw=red to the node to see it) surrounding all given coordinates:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
    \draw (-1,2) -- (5,2) -- (3,0) -- (0,0) -- cycle;
    \node [fit={(-1,2) (5,2) (3,0) (0,0)}] {x};
\end{tikzpicture}
\end{document}

rectangle fit

In some cases it could be an alternativ to use the shape library and set the dimensions to a node instead of using coordinates. To get the exact dimensions use inner sep=0pt additionally, otherwise the size will be advanced by the value of inner sep, which is 0.3333em ba default

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \node [minimum width=2cm, minimum height=1cm,draw] {x};
    \node at (3,0) [minimum width=2cm, minimum height=1cm,draw,trapezium] {x};
    \node at (6,0) [minimum width=2cm, minimum height=1cm,draw,ellipse] {x};
\end{tikzpicture}
\end{document}

use node shapes

When you use a node it is even possible to align another node on top of it by giving the first one a name:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \node (my node) [minimum width=2cm, minimum height=1cm,draw] {x};
    \node at (my node) [minimum width=1cm, minimum height=1cm,draw,circle] {X};
\end{tikzpicture}
\end{document}

anchoring named nodes

share|improve this answer
1  
fit fits my needs :). Thanks –  Johannes Feb 8 '13 at 14:37
    
If you want a path with a geometric shape it’s maybe better to use a shaped node with a given width. See my edit. –  Tobi Feb 8 '13 at 14:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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