10
\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.

  • 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
11

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

  • 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
2

I was trying to do something similar and ended up finding this question.

But I found a different solution for the case where your path is polygonal (i.e., formed by straight lines connecting points): Give a name to every vertex in your path (say using coordinate), and latter use a barycentric coordinate system and create a node with weight one for each point in the path. That is the center of mass. Bellow is a minimal example.

\documentclass{standalone}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}
    \draw(0,0) coordinate (P1) -- (1,2) coordinate(P2) -- (3,-1) coordinate(P3) -- (2,-1) coordinate (P4) -- cycle;%%the cycle here does not affect the position of X.

    \coordinate (center) at (barycentric cs:P1=1,P2=1,P3=1,P4=1) {};
    \node at (center) {X};
\end{tikzpicture}
\end{document}

center of mass

  • 1
    But be careful about having colinear vertices in the path. This will affect the position of the center, as the weight are in the vertices. – Fabricio Feb 2 at 2:56

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.