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.

Is foreach really a tikz statement or is derived from some other package?

I would like to draw a horizontal axis with 7 ticks spaced 1 cm apart. I'd like to place a label under each tick that is the ordinal number of the tick (1,2,3..) and a label above the tick that is the value of the ordinal number multiplied by the constant 3.14

share|improve this question

1 Answer 1

Yes, foreach is a TikZ/PGF statement. It is described in the very detailed pgfmanual. You can also use it independently of TikZ/PGF by issuing \usepackage{pgffor} in your preamble. This is not necessary if you're using TikZ, as it will be loaded automatically.

Here's an example of how to achieve what you described in your question. \pgfmathsetmacro<macroname>{<expression>} uses the PGF math engine to do the calculation and assigns the result to a macro, while \pgfmathprintnumber rounds and outputs the result.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}

\foreach \x in {1,...,7} {
    \pgfmathsetmacro\result{\x * pi}
    \draw (\x,-4pt) -- (\x,4pt)
        node [below,yshift=-2ex] {\x}
        node [above] {\pgfmathprintnumber{\result}};
}

\draw [-latex] (1,0) -- (7.6,0);

\end{tikzpicture}
\end{document}

axis with labels

share|improve this answer
    
thanks, Jake. Is there an implicit loop index in foreach that can be referenced by the user? –  user5504 May 13 '11 at 17:50
3  
You can use /pgf/foreach/count for that. Write something like \foreach \x [count=\i] in {a,...,z} and \i will hold the loop counter (e.g. 1 for a, 2 for b etc.). –  Jannis Pohlmann May 13 '11 at 23:18

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.