5

I'm trying to draw a simple rectangle together with the dimensions. Here is my first try:

\documentclass{article}

\usepackage{tikz}

\begin{document}
    \pgfmathsetmacro{\a}{2.0}
    \pgfmathsetmacro{\b}{4.0}
    \begin{tikzpicture}
        \draw (0, 0) rectangle (\a, \b);
        \draw[|<->|] (0, 0) -- ++ (\a, 0); 
        \node[below] at (0.5*\a, 0) {a};
    \end{tikzpicture}
\end{document}

rectangle

This of course results in the line drawn on top of the rectangle, but I would like it to be a bit below. Sure, I could do something like

\draw[|<->|] (0, -5mm) -- ++ (\a, 0); 

but this is neither very nice nor does it scale correctly. Any suggestions?

Edit: Heres is an example of what I mean by not scaling correctly:

\documentclass{article}

\pagestyle{empty}
\usepackage{tikz}

\begin{document}
    \pgfmathsetmacro{\a}{2.0}
    \pgfmathsetmacro{\b}{4.0}
    \begin{tikzpicture}[scale=1]
        \draw (0, 0) rectangle (\a, \b);
        \draw[|<->|] ([yshift=-1mm]0, 0) -- ++ (\a, 0); 
        \node[below] at (0.5*\a, 0) {a};
    \end{tikzpicture}
    \begin{tikzpicture}[scale=5]
        \draw (0, 0) rectangle (\a, \b);
        \draw[|<->|] ([yshift=-1mm]0, 0) -- ++ (\a, 0); 
        \node[below] at (0.5*\a, 0) {a};
    \end{tikzpicture}
\end{document}

two rectangles

Psirus
  • 5,835
  • I've been looking for a long time for an automated way of doing this, I thought that a measure key would be great, with an optional distance, e.g., \draw[measure] (0,0) -- (1,0); automated, and [measure=1cm] in case one wants to decide the distance, but I don't know how to program it. – Manuel Feb 13 '15 at 10:53
  • Yes that is sort of what I had in mind. Although simply measure won't do, because how would it know when to place it above, below, left or right? Regardless, my tikz foo is too weak, hence this question. – Psirus Feb 13 '15 at 11:02
  • Yep, it's not perfect, I meant that something of this sort would be possible, ideally it would enable not only the distance (there's no above,below,left,right, you give two coordinates and then a shift), but also the label, and a few more things in my case. – Manuel Feb 13 '15 at 11:05
  • Is this relevant http://tex.stackexchange.com/questions/123913/adding-dimensions-to-tikz-pictures ? You can adjust the dimension presentation to an arrow rather than a node on the path. – percusse Feb 13 '15 at 12:21
  • Not quite, but http://tex.stackexchange.com/questions/14901/dimensioning-of-a-technical-drawing-in-tikz seems relevant. I'll take a look at that later. – Psirus Feb 13 '15 at 12:31

1 Answers1

2

Here is a layman's workaround where we define a node at (0,0) and then draw the arrow from its south.

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \pgfmathsetmacro{\a}{2.0}
    \pgfmathsetmacro{\b}{4.0}
    \begin{tikzpicture}[scale=1]
        \draw (0, 0) node[below] (A){} rectangle (\a, \b);
        \draw[|<->|] (A.south) -- ++ (\a, 0);
        \node[below] at (0.5*\a, 0) {a};
    \end{tikzpicture}
    \begin{tikzpicture}[scale=3]
        \draw (0, 0) node[below] (A){} rectangle (\a, \b);
        \draw[|<->|] (A.south) -- ++ (\a, 0);
        \node[below] at (0.5*\a, 0) {a};
    \end{tikzpicture}
\end{document}

enter image description here

You can vary the distance by changing the inner sep

\draw (0, 0) node[inner sep=1mm,below] (A){} rectangle (\a, \b);

enter image description here

  • 1
    But this is still a hardcoded number. If I were to scale the tikzpicture, the gap would change. On the contrary, the gap between the a and the rectangle stays the same, because of some magic in \node[below], I presume. – Psirus Feb 13 '15 at 10:31
  • @Psirus Can show show some code that it changes with scaling? I am not able to reproduce it. For example: \begin{tikzpicture}[scale=2] doesn't do that. –  Feb 13 '15 at 10:37
  • @Psirus Now it isn't hardcoded. What you observed can be used. –  Feb 13 '15 at 13:04