Take the 2-minute tour ×
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.

I want to generate a TikZ image from a foreach loop, where some coordinates change within this loop. How can I do something as simple as

float x = 0.5;
x += 0.1;

in TikZ/LaTeX? I searched on tex.stackexchange and the TikZ manual, but I only found counters (which are integer..).


I want to draw a barplot like this one: http://www.statistiker-wg.de/pgf/tutorials/barplot.htm (bottom of the page, pdf file: http://www.statistiker-wg.de/pgf/pics/barplot4.pdf), but I don't want to specify the x coordinates manually.

  \definecolor{myblue}{HTML}{92dcec}

  \begin{tikzpicture}

  \draw (0cm,0cm) -- (15.5cm,0cm);  %Abzisse
  \draw (0cm,0cm) -- (0cm,-0.1cm);  %linkes Ende der Abzisse
  \draw (15.5cm,0cm) -- (15.5cm,-0.1cm);  %rechtes Ende der Abzisse

  \draw (-0.1cm,0cm) -- (-0.1cm,4.5cm);  %Ordinate
  \draw (-0.1cm,0cm) -- (-0.2cm,0cm);  %unteres Ende der Ordinate
  \draw (-0.1cm,4.5cm) -- (-0.2cm,4.5cm) node [left] {\%};  %oberes Ende der Ordinate

  \foreach \x in {1,...,4}  %Hilfslinien
    \draw[gray!50, text=black] (-0.2 cm,\x cm) -- (15.5 cm,\x cm) 
      node at (-0.5 cm,\x cm) {\x};  %Beschriftung der Hilfslinien

    \node at (7.5cm,5cm) {Wachstumsrate des realen BIP für die zehn
                           Bevölkerungsreichsten Staaten der EU 2005};  %Überschrift

  \foreach \x/\y/\country in {0.5/4.1/Rumänien,  %\x ist Anfang der Säulen
                              2/3.7/Griechenland,  %\y ist Höhe der Säulen
                              3.5/3.5/Spanien,
                              5/3.5/Polen,
                              6.5/1.9/Großbritannien,
                              8/1.5/Niederlande,
                              9.5/1.2/Frankreich,
                              11/0.9/Deutschland,
                              12.5/0.5/Portugal,
                              14/0.1/Italien}
    {
     \draw[fill=myblue] (\x cm,0cm) rectangle (1cm+\x cm,\y cm) %die Säulen
       node at (0.5cm + \x cm,\y cm + 0.3cm) {\y}; %die Prozente über den Säulen
     \node[rotate=45, left] at (0.6 cm +\x cm,-0.1cm) {\country}; %Säulenbeschriftung
    };

\end{tikzpicture}
share|improve this question
2  
Can you show a simple loop in a MWE? What is the loop variable etc. Best is to include a toy example that starts with \documentclass{...} to \end{document} where you have a loop idea (not necessarily fully functional) in it. –  percusse Dec 16 '12 at 15:37
1  
It might be a good idea to explain in a bit more detail what you're trying to do. In general, though, you could for instance use the PGF math engine (\def\x{0.5}\pgfmathsetmacro\x{\x+0.1}), or use dimensions (\newlength\x \setlength{\x}{0.5pt} \addtolength{\x}{0.1pt} \the\x). –  Jake Dec 16 '12 at 15:40
1  
I would recommend to use the pgfplots package for this, instead of hand-knitting everything yourself. –  Jake Dec 16 '12 at 16:16
    
The pgfmathsetmacro doesn't seem to work inside a loop (it is only executed once). I don't want to use pgfplots, because I also want to use variables in other cases -- not only in plots. –  Khal Dec 16 '12 at 19:51
    
Maybe l3float can be of use here? –  You Dec 16 '12 at 22:18

2 Answers 2

I have to agree with Jake that the example on the linked website is probably done in the absence of pgfplots and in my opinion very very tedious. Moreover, reusing the data is much easier with a pgfplotstable solution.

Here is the TikZ version without manual entries (I've used LuaLaTeX or XeLaTeX , I don't know what you are using, maybe inputenc)

\documentclass[tikz,border=5mm]{standalone}
\usepackage{fontspec}
\definecolor{myblue}{HTML}{92dcec}
\begin{document}
  \begin{tikzpicture}

  \draw (0cm,0cm) -- (15.5cm,0cm);  %Abzisse
  \draw (0cm,0cm) -- (0cm,-0.1cm);  %linkes Ende der Abzisse
  \draw (15.5cm,0cm) -- (15.5cm,-0.1cm);  %rechtes Ende der Abzisse

  \draw (-0.1cm,0cm) -- (-0.1cm,4.5cm);  %Ordinate
  \draw (-0.1cm,0cm) -- (-0.2cm,0cm);  %unteres Ende der Ordinate
  \draw (-0.1cm,4.5cm) -- (-0.2cm,4.5cm) node [left] {\%};  %oberes Ende der Ordinate

  \foreach \x in {1,...,4}  %Hilfslinien
    \draw[gray!50, text=black] (-0.2 cm,\x cm) -- (15.5 cm,\x cm) 
      node at (-0.5 cm,\x cm) {\x};  %Beschriftung der Hilfslinien

    \node at (7.5cm,5cm) {Wachstumsrate des realen BIP für die zehn
                           Bevölkerungsreichsten Staaten der EU 2005};  %Überschrift

  \foreach \y/\country[count=\x from 0] in {4.1/Rumänien,  %\x ist Anfang der Säulen
                              3.7/Griechenland,  %\y ist Höhe der Säulen
                              3.5/Spanien,
                              3.5/Polen,
                              1.9/Großbritannien,
                              1.5/Niederlande,
                              1.2/Frankreich,
                              0.9/Deutschland,
                              0.5/Portugal,
                              0.1/Italien}
    {
     \draw[fill=myblue,xshift=0.5cm] (0.5 + 1.5*\x cm,0cm) rectangle (1cm+1.5*\x cm,\y cm) %die Säulen
       node at (0.5cm + 1.5*\x cm,\y cm + 0.3cm) {\y}; %die Prozente über den Säulen
     \node[rotate=45, left] at (1.15 cm + 1.5*\x cm,-0.1cm) {\country}; %Säulenbeschriftung
    };
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Not a direct answer to the question, but an example of how PGFPlots could be used for this:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.7}
\usepackage[utf8]{inputenc}

\begin{document}
\pgfplotstableread[col sep=comma]{
Land, Wert
Rumänien, 4.1
Griechenland, 3.7
Spanien, 3.5
Polen, 3.5
Grossbritannien, 1.9
Niederlande, 1.5
Frankreich, 1.2
Deutschland, 0.9
Portugal, 0.5
Italien, 0.1
}\datatable

\begin{tikzpicture}
\begin{axis}[   
    x=10mm,
    ybar, bar width=6mm,
    ymin=0,
    xtick=data,
    every x tick label/.style={
        anchor=east,
        rotate=45
    },
    xticklabels from table={\datatable}{Land},
    nodes near coords,
    axis lines*=left,
    ylabel=\%, ylabel style={rotate=-90},
    title style={align=center},
    title={Wachstumsrate des realen BIP für die\\ zehn                            bevölkerungsreichsten Staaten der EU 2005}
]
\addplot [black, fill=cyan] table [col sep=comma, x expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
I can't believe I didn't include the pgfplots version. Thanks! –  percusse Dec 17 '12 at 10:56

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.