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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to draw a simple figure with tikz:

enter image description here

Thanks for your help

This is what I have so far:

%\documentclass[tikz]{standalone}
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw[very thick,->] (0,0) -- (10,0);
\draw[blue!80,fill=blue!50] (2,0) circle (.2cm);
\draw[blue!80,fill=blue!50] (4,0) circle (.2cm);
\draw[blue!80,fill=blue!50] (6,0) circle (.2cm);
\draw[blue!80,fill=blue!50] (8,0) circle (.2cm);
\end{tikzpicture}

\end{document}
share|improve this question
up vote 7 down vote accepted

If you draw the circles as circular nodes, then you can use the label syntax to place the labels above and below them.

\documentclass[border=5pt, multi, tikz]{standalone}

Let's load some fancier arrow tips to improve on the current arrow.

\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}

If we are going to draw the circles as nodes, it makes sense to define a standard style with the required settings to avoid having to repeat the same settings and to make it easier to modify them, if required.

  [
    my blue/.style={blue!80,  fill=blue!50, minimum width=4mm, circle, inner sep=0pt},

This should draw circular nodes which look pretty much like the circles defined in the original code.

We want sans text so let's set that for the picture.

    font=\sffamily
  ]

We start as before, but we substitute one of those fancier arrow tips for the plain one, using -{Stealth[]}.

  \draw[very thick,-{Stealth[]}] (0,0) -- (10,0)

We could place each node separately using

\node [my blue, label={[text=blue!80]above:<label>}, label={[text=blue!80]below:<label>}] at <location> {};

but that's a lot of typing, so let's use a loop to draw the 4 circles we want and place the labels. The lower labels we can derive by adding a 0 to the number of the step: 10, 20, 30 and so on. So we want a variable for the upper labels - \i say - and another one to keep count - \n say. Then our loop will look like this:

    foreach \i [count=\n] in {AA,BB,CC,DD}
    {
      node [pos=\n/5, my blue, label={[text=blue!80]above:\i}, label=below:\n 0] {}
    } 

The position for each node is also calculated, as shown here, from \n. So this is a pretty efficient way to draw the circles and label them.

While we are here, we might as well add a coordinate where we want the 'f1` marker to go, in the middle of the line, but it will be easier to add this marking as an additional step.

      coordinate [midway] (f1) 

Finally, we add a node to the right of the very end of the line for the 'temp' label.

      node [right] {temp};

Now we just need to add the vertical marking and the label 'f1' where we placed the coordinate (f1) above. We can do this using relative coordinates using the + syntax. If we draw downwards, then the label node can simply be added below.

  \draw [very thick] (f1) ++(0,2mm) -- (f1) -- ++(0,-2mm) node [below] {f\textsubscript{1}};

We use a \textsubscript rather than $f_1$ because, this way, we keep to sans serif as desired.

\end{tikzpicture}
\end{document}

And we are done!

temp arrow

Complete code:

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
  [
    my blue/.style={blue!80,  fill=blue!50, minimum width=4mm, circle, inner sep=0pt},
    font=\sffamily
  ]
  \draw[very thick,-{Stealth[]}] (0,0) -- (10,0)
    foreach \i [count=\n] in {AA,BB,CC,DD}
    {
      node [pos=\n/5, my blue, label={[text=blue!80]above:\i}, label=below:\n 0] {}
    } coordinate [midway] (f1) node [right] {temp};
  \draw [very thick] (f1) ++(0,2mm) -- (f1) -- ++(0,-2mm) node [below] {f\textsubscript{1}};
\end{tikzpicture}
\end{document}

EDIT

I thought I would add the following method to produce the diagram with a single line of TikZ. This uses the decorations.markings library in place of relying on a second line of TikZ code. Although I thought of doing this originally, I'm not convinced it is worth the trouble for a single line. Nonetheless, following an offer of code to implement basically the same idea, I thought it might be useful to show how to do it with the existing library.

As well as deleting the second \draw command, we can eliminate the coordinate specification for (f1) as we don't need it. Instead, we add the node and the vertical line as a decoration, which we apply to the first \draw as a postaction. This means that the decoration is drawn as well as the original line, as we want, and not instead of it, as it would be if we simply said decorate.

The additional options needed for the \draw are

postaction=decorate, decoration={markings, mark=at position .5 with {\arrow{|}\node [below, yshift=-1mm, anchor=north] {f\textsubscript{1}}; } }

The contents of decoration specifies the type of decoration, markings, and the settings for that decoration. mark=at position .5 puts it at the midway point and with {<stuff>} specifies what to use as the marking. The \arrow{<arrow tip>} is specifically provided by the decorations.markings library for the easy use of arrow tips as markings. We then add the node below it, shifted down a little, as before.

The result:

marked temp line in one step

Complete code:

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{arrows.meta,decorations.markings}
\begin{document}
\begin{tikzpicture}
  [
    my blue/.style={blue!80,  fill=blue!50, minimum width=4mm, circle, inner sep=0pt},
    font=\sffamily
  ]
  \draw[very thick, -{Stealth[]}, postaction=decorate, decoration={markings, mark=at position .5 with {\arrow{|}\node [below, yshift=-1mm, anchor=north] {f\textsubscript{1}}; } }] (0,0) -- (10,0)
    foreach \i [count=\n] in {AA,BB,CC,DD}
    {
      node [pos=\n/5, my blue, label={[text=blue!80]above:\i}, label=below:\n 0] {}
    } node [right] {temp};
\end{tikzpicture}
\end{document}
share|improve this answer

You can decorate a path with the syntax node [location] {contents}. For example, location can be above or right=10pt.

\documentclass{article}
\usepackage{tikz}
% to reduce typing
\newcommand{\decor}[2]{node [above=10pt] {#1} node [below=10pt]
  {\textcolor{black}{#2}}}

\begin{document}
\begin{tikzpicture}
\draw[very thick, ->] (0,0) -- (10,0) node [right] {temp};
\draw[blue!80,fill=blue!50] (2,0) circle (.2cm) \decor{AA}{10};
\draw[blue!80,fill=blue!50] (4,0) circle (.2cm) \decor{BB}{20};
\draw[blue!80,fill=blue!50] (6,0) circle (.2cm) \decor{CC}{30};
\draw[blue!80,fill=blue!50] (8,0) circle (.2cm) \decor{DD}{40};
\draw[very thick] (5,0.5) -- (5,-0.5) node [below] {\(f_1\)};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
2  
you can add text=black in the second node of \decor – percusse Jan 9 at 8:58

You can do it with a loop. Here is some commented version

\begin{tikzpicture}
\draw[very thick,-latex] (0,0) -- (8,0);% That arrow head is called 'latex'

\foreach \x % do things multiple times with index \x
  [count=\xi] % when going through the list also count the index with \xi
  in {A,...,D}{ % tikz understands alphabetical sequences
    \node[fill=blue!10,circle,% give the node options
          label={[text=blue!10]90:\x\x},% put the letters
          label={[text=blue!10]270:\xi0},
          inner sep=2mm%increase the size a bit
          ]
          (n-\xi) % give a name
          at (2*\xi-1,0) % place them at odd numbered x's
          {}; % Nothing is written inside the disks
}
\end{tikzpicture}
share|improve this answer

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.