10

Edit: In response to the comments, the question has been edited to include the MWE. The accepted answer (by Gonzalo) is still valid.

Here's my code:

\documentclass{article}


% *** MISC UTILITY PACKAGES ***
\usepackage{graphicx}
\usepackage[caption=false]{subfig}%The caption option has been disabled
%to avoid conflict with the IEEEtran class file.
\usepackage{tikz}

% *** MATH PACKAGES ***
\usepackage[cmex10]{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{figure}[!t]
    \centering
\begin{tikzpicture} 
\node[anchor=south west, inner sep = 0] (image) at (0,0)
{\includegraphics[width=8cm]{Tulips.jpg}};
\begin {scope}[x={(image.south east)},y={(image.north west)}]
   \fill [white] (0,0) rectangle (1,1);
   \foreach \y in {0,1,...,8} { \node [anchor=east] at (0.09,0.07+0.88*\y/8) {\small {-5+5*\y}}; }
\end {scope}
\end{tikzpicture}
\caption{Nothing} 
\label{nothing}
\end{figure}
\end{document}

Here's the result:
enter image description here


I wanted the series to appear as {-5,0,5,...35}. What's being shown is {-5+5*0,-5+5*1,-5+5*2,...-5+5*8}.

How to evaluate the series before it gets printed as text?

  • 3
    Please, please, please: Post full minimal documents instead of just snippets. Finding answers is fun, completing documents is not. – Jake Sep 27 '12 at 18:12
  • Really sorry about that. I will post MWE soon. – Shashank Sawant Sep 27 '12 at 18:23
  • 3
    @Jake: That's an excellent way to put it. – Loop Space Sep 27 '12 at 18:26
  • 1
    @GonzaloMedina Thanks! At Jake and Andrew: Since this wasn't a bug I wanted to know the command that could do the job (in hindsight I can say it's \yeval). I thought of it more as a theory question. I have learnt a lot from answers you guys have posted (perhaps more than any book) and I agree that MWEs help. People having similar problems can simply copy the MWEs to understand Latex. Hence I have modified question to include the MWE. Sincerest apologies for not including it to begin with. – Shashank Sawant Sep 27 '12 at 19:14
  • 1
    @ShashankSawant: I have resorted to using \pgfmathsetmacro{\<name>}{<expression>} to compute expression and has found that quite useful. The evaluate= is clearly better for this case, but you should keep \pgfmathsetmacro in mind. – Peter Grill Sep 27 '12 at 20:45
13

You can use the evaluate=<variable> as <macro> using <formula> syntax:

\documentclass{scrartcl}
\usepackage{tikz,pgf}
\usepackage{graphicx}

\begin{document}

\begin{tikzpicture} 
\node[anchor=south west, inner sep = 0] (image) at (0,0)
{\includegraphics[width=8cm]{cat}};
\begin {scope}[x={(image.south east)},y={(image.north west)}]
  \draw [help lines,lime,xstep=.01,ystep=.01] (0,0) grid (1,1);
  \draw [help lines,orange,xstep=.05,ystep=.05] (0,0) grid (1,1);
  \draw [help lines,red,xstep=.1,ystep=.1] (0,0) grid (1,1);
  \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
  \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }

   \fill [white] (0,0) rectangle (1,1);
  \foreach \y [evaluate=\y as \yeval using -5+5*\y] in {0,...,8} { \node [anchor=east,font=\tiny] at (0.09,0.07+0.88*\y/8) {\pgfmathtruncatemacro{\yet}{\yeval}\yet}; }
\end {scope}
\end{tikzpicture}

\end{document}

enter image description here

3

Answer to the modified question:

\documentclass{article}


% *** MISC UTILITY PACKAGES ***
\usepackage{graphicx}
\usepackage[caption=false]{subfig}%The caption option has been disabled
%to avoid conflict with the IEEEtran class file.
\usepackage{tikz}

% *** MATH PACKAGES ***
\usepackage[cmex10]{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{figure}[!t]
    \centering
\begin{tikzpicture} 
\node[anchor=south west, inner sep = 0] (image) at (0,0)
{\includegraphics[width=8cm]{Tulips.jpg}};
\begin {scope}[x={(image.south east)},y={(image.north west)}]
   \fill [white] (0,0) rectangle (1,1);
%   \foreach \y in {0,1,...,8} { \node [anchor=east] at (0.09,0.07+0.88*\y/8) {\small {-5+5*\y}}; }
\foreach \y [evaluate=\y as \yeval using -5+5*\y] in {0,...,8} { \node [anchor=east,font=\small] at (0.09,0.07+0.88*\y/8) {\yeval}; }
\end {scope}
\end{tikzpicture}
\caption{Nothing} 
\label{nothing}
\end{figure}
\end{document}

The output looks like this: enter image description here

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.