如何在打印为文本之前评估 tikz 表达式?

如何在打印为文本之前评估 tikz 表达式?

编辑:针对评论,已对问​​题进行了编辑,以包含 MWE。已接受的答案(由 Gonzalo 提供)仍然有效。

这是我的代码:

\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}

结果如下:
在此处输入图片描述


我希望该系列显示为 {-5,0,5,...35}。显示的是 {-5+5*0,-5+5*1,-5+5*2,...-5+5*8}。

如何在将该系列打印为文本之前对其进行评估?

答案1

您可以使用以下evaluate=<variable> as <macro> using <formula>语法:

\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}

在此处输入图片描述

答案2

修改后问题的答案:

\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}

输出如下所示: 在此处输入图片描述

相关内容