Graphics, Figures & TablesAn arrow with a text on it

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tush
Posts: 11
Joined: Fri Feb 22, 2019 2:54 pm

An arrow with a text on it

Post by tush »

I want to draw the following:
Capture.PNG
Capture.PNG (3.14 KiB) Viewed 14208 times
Something that resembles a bit engineering drawings that specify object's dimensions.

The code I have at the moment is this:

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (6,0) -- (6,3) -- (0,3) -- cycle;
\draw[<->] (0,3.3) -- (6,3.3);
\node[text width=2cm,fill=white] at (3,3.3) {$l=2 m$};
\end{tikzpicture}
\end{document}
Certainly, if I change the y coordinate of the arrow, I should do the same for the text node.

Is there a graphics primitive that draws an arrow with a text node in its middle?

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

An arrow with a text on it

Post by Stefan Kottwitz »

Hi tush,

here is a quick way with the quotes library syntax:

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}
\tikzset{every edge quotes/.style =
          { fill = white,
            sloped,
            execute at begin node = $,
            execute at end node   = $  }}
\begin{document}
\begin{tikzpicture}
  \draw[<->] (0,0) to ["l_1 = 2 m"] (6,0) ;
  \draw[<->] (0,0) to ["l_2 = 1 m"] (0,3) ;
\end{tikzpicture}
\end{document}
Commonly metric units are written in upright shape, to distinguish from (italic) variables. There's the siunitx package that does proper unit typesetting, I use it here:

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}
\usepackage{siunitx}
\tikzset{every edge quotes/.style =
          { fill = white,
            sloped,
            execute at begin node = $,
            execute at end node   = $  }}
\begin{document}
\begin{tikzpicture}
  \draw[<->] (0,0) to ["l_1 = \SI{2}{m}"] (6,0) ;
  \draw[<->] (0,0) to ["l_2 = \SI{1}{m}"] (0,3) ;
\end{tikzpicture}
\end{document}
Click on "Run LaTeX here" to see the output.

Stefan
LaTeX.org admin
tush
Posts: 11
Joined: Fri Feb 22, 2019 2:54 pm

An arrow with a text on it

Post by tush »

Thanks a lot Stefan!
I wasn't familiar with the quotes library. Thanks for illustrating its usage.

And yes, I am aware of the siunitx package, it is just that I wanted to keep my example minimal.
Post Reply