1

How to draw the following diagram in latex?

I would be grateful to know how to put another text above 100 (for example 200 above 100 and $AV_{X}$ above $AV_{Y}$)?

image

2

3 Answers 3

4

If you add align=center (or align=left or align=right) to the options of a node, you can use \\ in the node text to add line breaks.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \draw (0,0) -- (2,0);
    % \x/\y/\z = label below/label above/label on top
    \foreach \x/\y/\z in {0/100/200,1//,2/$AV_Y$\rlap{=?}/$AV_X$}
      \draw (\x,-2mm) node[below]{\x} -- ++(0,4mm) node[above,align=center] {\z\\\y};
  \end{tikzpicture}
\end{document}
3

A small variation of @js bibra answer (+1):

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}
\draw[thick] (0,0) -- (4,0);
\foreach \i [count=\x from 0] in {100, ,$AV_Y{=}?$} 
        {
\draw[thick] (2*\x,2mm) node[above] {\i} -- ++ (0,-4mm) node[below] {\x};
        }
    \end{tikzpicture}
\end{document}

enter image description here

Addendum: In above answer I forgot on nodes in the second row above ticks. They can be add as two line content of nodes. For this nodes should have option align=center as suggest @gernot in his answer (+1):

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[every node/.append style={align=center}]
\draw[thick] (0,0) -- (4,0);
\foreach \i [count=\x from 0] in {200\\100, ,$AV_X$\\$AV_Y{=}?$}
        {
\draw[thick] (2*\x,2mm) node[above] {\i} -- ++ (0,-4mm) node[below] {\x};
        }
    \end{tikzpicture}
\end{document}

enter image description here

3
3

enter image description here

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{siunitx}
\usetikzlibrary{positioning,calc}
\begin{document}
    \begin{tikzpicture}
        \draw[line width=1pt] (0,0)node(a){} -- (8,0)node(b){}[right=4mm];
        \foreach \x/\y in {100/0,/1,{$AV_Y$}=?/2}%
        {
            \draw[line width=1pt] (4*\y,-2mm) node[below] {\y} -- ++(0,4mm) node[above] {\x};
        }
    \end{tikzpicture}
\end{document}

To have additional text above the start and end labels which are currently present (i.e: 200 over 100)

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{siunitx}
\usetikzlibrary{positioning,calc}
\begin{document}
    \begin{tikzpicture}
        \draw[line width=1pt] (0,0)node(a){} -- (8,0)node(b){}[right=4mm];
        \foreach \x/\y/\z in {100/0/200,\phantom{1}/1/\phantom{1},{$AV_Y$}=?/2/{$AV_X$}\phantom{=?}}%
        {
            \draw[line width=1pt] (4*\y,-2mm) node[below]() {\y} -- ++(0,4mm) node[above](n\y) {\x};
            \node[above=1ex of n\y] {\z};
        }
    \end{tikzpicture}
\end{document}

enter image description here

3

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.