1

I'd like to have the labels for the x-values go 0,0.1,0.2,0.3 and then go up to 1.6 (which is at 8cm). However I don't understand the \xtext-method well enough. Could someone please point out how the labels can be automatically put with the \foreach-command?

MWE

\documentclass{scrartcl}

\usepackage{tikz,relsize,amsmath}
\usetikzlibrary{calc,trees,shadows,positioning,arrows,chains,shapes.geometric,shapes,matrix,shapes.symbols,patterns}

\usepackage[T1]{fontenc}
\usepackage{tgheros}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %Netz
    \draw[style=help lines,step=0.5cm] (0,0) grid (8.1,10.1);
    %Achsentitel (Legende)
    \draw[->,thick] (-0.1,0) -- (8.4,0) node[anchor=west] {\(\frac{\text{x}_{\text{1}}}{\text{y}}\)}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,10.4) node[anchor=east] {\(\frac{\text{x}_{\text{2}}}{\text{y}}\)}; %Y-Achse
    %ticks an Achsen
    \foreach \x in {0,1,...,9} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,10} \draw [thick](-2pt,\y) -- (2pt,\y);
    %Achsenbeschriftung (Zahlen an den Achsen)
    \foreach \x/\xtext in {0/0,1/0.2,...,8/1.6} \draw (\x cm, 0 cm) node[anchor=north] {\xtext};
    %\foreach \y in {0,1,...,10} \draw (0 cm, \y cm) node[anchor=east]{\y};
    %Stores
    \filldraw (4,7.5) circle (0.08cm) node[anchor=north west] {A};
\end{tikzpicture}
\end{center}
\end{document}
2
  • You can find more details in the paragraph Multiple variables. section 56 Repeating Things: The Foreach Statement of the pgfmanual. Jun 20, 2013 at 12:39
  • Yup, there is one on page 56 which contains some tangible content, but other than, unfortunately no clear pointers, at least in my eyes. Have you performed such a thing already by any chance?
    – henry
    Jun 20, 2013 at 12:46

1 Answer 1

2

Here's one possibility; the label is ome fifthe of the position, so you can use the evaluate=<variable> as <macro> using <formula> syntax (\pgfmathroundto was used to round the numbers):

\documentclass{scrartcl}

\usepackage{tikz,relsize,amsmath}
\usetikzlibrary{calc,trees,shadows,positioning,arrows,chains,shapes.geometric,shapes,matrix,shapes.symbols,patterns}

\usepackage[T1]{fontenc}
\usepackage{tgheros}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %Netz
    \draw[style=help lines,step=0.5cm] (0,0) grid (8.1,10.1);
    %Achsentitel (Legende)
    \draw[->,thick] (-0.1,0) -- (8.4,0) node[anchor=west] {\(\frac{\text{x}_{\text{1}}}{\text{y}}\)}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,10.4) node[anchor=east] {\(\frac{\text{x}_{\text{2}}}{\text{y}}\)}; %Y-Achse
    %ticks an Achsen
    \foreach \x in {0,1,...,9} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,10} \draw [thick](-2pt,\y) -- (2pt,\y);
    %Achsenbeschriftung (Zahlen an den Achsen)
    \foreach \x [evaluate=\x as \xval using (1/5)*\x] in {0,1,...,8} 
  \draw (\x cm, 0 cm) node[anchor=north] {\pgfmathroundto{\xval}\pgfmathresult};
    %\foreach \y in {0,1,...,10} \draw (0 cm, \y cm) node[anchor=east]{\y};
    %Stores
    \filldraw (4,7.5) circle (0.08cm) node[anchor=north west] {A};
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

3
  • Thank you. But I discovered a problem with other scales, meaning I actually can't use your solution anymore. Dealing with scales is a bit confusing really.
    – henry
    Jun 20, 2013 at 13:45
  • 1
    @henry if there's a functional relation between the variable and the label (as in your example), you can use it as I explained; otherwise, I'm afraid you have to give the complete explicit list when using multiple variables (in this case the ... syntax can't be used), as in \foreach \x/\text in {1/a,2/c,3/z,4/f,7/s,8/q} Jun 20, 2013 at 14:15
  • Since I kind of misjudged the whole 'planning' for setting up a TikZ graphic with a lot of data points, I implemented 2 scales which gave me quite a headache, so although there was some functional relation I was a bit overwhelmed. But I'll definitely keep that in mind!
    – henry
    Jun 20, 2013 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .