使用 \textwidth 指定长度时 Tikz 出错

使用 \textwidth 指定长度时 Tikz 出错

MWE如下:

\documentclass{article}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
        \newcommand{\s}{5}
        \begin{figure}
            \resizebox{\textwidth}{!}{
            \begin{tikzpicture}
                \fill[Goldenrod] (0,0) rectangle (0.6*\s, 0.6*\s);
                \fill[Goldenrod](0.6*\s, 0.45*\s) rectangle (0.8*\s, 0.55*\s);
                \fill[Goldenrod](0.6*\s, 0.25*\s) rectangle (0.8*\s, 0.35*\s);
                \fill[Goldenrod](0.6*\s, 0.05*\s) rectangle (0.8*\s, 0.15*\s);
                \draw (0.25*\s,0.3*\s)  edge[out=45, in=180, red]  (0.6*\s, 0.5*\textwidth);
                \draw (0.25*\s,0.3*\s) edge[out=0, in=180, green] (0.6*\s,0.3*\s);
                \draw (0.25*\s,0.3*\s) edge[out=-45, in=180, blue] (0.6*\s,0.1*\s);
                \node[text width=1cm, anchor=west] at (0.6*\s,0.5*\s) {\footnotesize  \color{red}$\lambda_1$};
                \node[text width=1cm, anchor=west] at (0.6*\s,0.3*\s) {\footnotesize  \color{green}$\lambda_2$};
                \node[text width=1cm, anchor=west] at (0.6*\s,0.1*\s) {\footnotesize  \color{blue}$\lambda_3$};
                \shade[inner color=white,outer color=Goldenrod] (0.3*\s,0.3*\s) circle (0.2*\s);
            \end{tikzpicture}
            }
        \end{figure} 
\end{document}

新定义的变量\s不会导致任何问题,但是使用\textwidth指定坐标会使 Tikz 抛出错误(我直接从 TexStudio 复制):

line 22: Missing \endcsname inserted. }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }
line 22: Package PGF Math Error: Unknown operator `.' or `.1' (in '0.5*\textwidth .180'). }

我非常好奇这背后的原因,如果有人能解释一下我会很高兴。

答案1

\textwidth您可以通过使用相对(而不是绝对)坐标规范来实现想要的效果。

\documentclass{article}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\newcommand{\s}{5}
\begin{figure}
  \resizebox{\textwidth}{!}{
    \begin{tikzpicture}
      \fill[Goldenrod] (0,0) rectangle (0.6*\s, 0.6*\s);
      \fill[Goldenrod](0.6*\s, 0.45*\s) rectangle (0.8*\s, 0.55*\s);
      \fill[Goldenrod](0.6*\s, 0.25*\s) rectangle (0.8*\s, 0.35*\s);
      \fill[Goldenrod](0.6*\s, 0.05*\s) rectangle (0.8*\s, 0.15*\s);
      \draw (0.25*\s,0.3*\s)  edge[out=45, in=180, red]  +(0.35*\s,{0.5*\textwidth-0.3*\s});
      \draw (0.25*\s,0.3*\s) edge[out=0, in=180, green] (0.6*\s,0.3*\s);
      \draw (0.25*\s,0.3*\s) edge[out=-45, in=180, blue] (0.6*\s,0.1*\s);
      \node[text width=1cm, anchor=west] at (0.6*\s,0.5*\s) {\footnotesize  \color{red}$\lambda_1$};
      \node[text width=1cm, anchor=west] at (0.6*\s,0.3*\s) {\footnotesize  \color{green}$\lambda_2$};
      \node[text width=1cm, anchor=west] at (0.6*\s,0.1*\s) {\footnotesize  \color{blue}$\lambda_3$};
      \shade[inner color=white,outer color=Goldenrod] (0.3*\s,0.3*\s) circle (0.2*\s);
    \end{tikzpicture}
  }
\end{figure}
\end{document}

相对坐标指定

font=\footnotesize, text=blue请注意,使用“例如”而不是\footnotesize \color{blue}“等等”可能更为明智。\color除非您真的需要,否则请特别谨慎使用“,因为没有它您就无法完成所需的工作。

相关内容