如何限制维度的长度?使用 pgfmathgreater 和 ifthenelse 会导致错误:不完整 \iffalse;第 32 行之后的所有文本都被忽略

如何限制维度的长度?使用 pgfmathgreater 和 ifthenelse 会导致错误:不完整 \iffalse;第 32 行之后的所有文本都被忽略

我想限制维度的长度。但使用 pgfmathgreater 和 ifthenelse 会导致错误。最小非工作示例:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfmath}
\usepackage{ifthen}

\usetikzlibrary{
  arrows.meta,
}

\tikzset{
  pics/my-pic/.style args={#1}{
    code={
      \pgfmathsetlengthmacro{\arrowLength}{#1}

      \pgfmathsetlengthmacro{\arrowHeadWidth}{\arrowLength / 6}
      \ifthenelse{\pgfmathgreater{\arrowLength}{.5cm} < 1}{
%      \ifthenelse{0>1}{
              \pgfmathsetlengthmacro{\arrowHeadWidth}{10pt}
      }{}
      \pgfmathsetlengthmacro{\arrowHeadLength}{\arrowHeadWidth / 1.4}

      \draw[-{Latex[round,width=\arrowHeadWidth,length=\arrowHeadLength]}] (0, 0) -- (\arrowLength, 0);
    }
  },
  pics/my-pic/.default=.65cm,
}

\begin{document}
\begin{tikzpicture}
  \pic {my-pic};
\end{tikzpicture}
\end{document}

错误是

! Incomplete \iffalse; all text was ignored after line 32.
<inserted text> 
\fi

答案1

您不能传递\pgfmathgreater{\arrowLength}{.5cm}\ifthenelse,因为后者需要扩展为整数的东西,而不是设置\pgfmathresult为整数的指令。

它更简单。

\documentclass{standalone}

\usepackage{tikz}

\usetikzlibrary{
  arrows.meta,
}

\tikzset{
  pics/my-pic/.style args={#1}{
    code={
      \pgfmathsetlengthmacro{\arrowLength}{#1}
      \pgfmathsetlengthmacro{\arrowHeadWidth}{\arrowLength / 6}
      \ifdim\arrowLength>.5cm
        \pgfmathsetlengthmacro{\arrowHeadWidth}{10pt}
      \fi
      \pgfmathsetlengthmacro{\arrowHeadLength}{\arrowHeadWidth / 1.4}

      \draw[-{Latex[round,width=\arrowHeadWidth,length=\arrowHeadLength]}] (0, 0)
        -- (\arrowLength, 0);
    }
  },
  pics/my-pic/.default=.65cm,
}

\begin{document}
\begin{tikzpicture}
  \pic {my-pic};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容