我怎样才能计算样式值?

我怎样才能计算样式值?

是否可以计算样式值(加法等)?我想做以下事情:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
    \draw (#2.south east) to [in=90, out=90, looseness=0.85+#1] (#3.south west);
}

\begin{document}    
\begin{tikzpicture}[start chain]
    \node[on chain] (k11) {};
    \node[on chain] (k12) {};
    \mypath{k11}{k12}       
\end{tikzpicture}    
\end{document}

问题是which 与和 一起looseness=0.85+#1失败。Missing number, treated as zeroIllegal unit of measure (pt inserted)

答案1

可以将数字解析为宏,并将宏用作键的参数。

但是,必须这样做的原因是looseness密钥似乎无法使用 进行解析pgfmath。因此,对于黑客爱好者来说:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\makeatletter
\def\tikz@to@set@in@looseness#1{%
  \pgfmathparse{#1}\let\tikz@to@in@looseness=\pgfmathresult%
  \let\tikz@to@end@compute=\tikz@to@end@compute@looseness%
  \tikz@to@switch@on%
}
\def\tikz@to@set@out@looseness#1{%
  \pgfmathparse{#1}\let\tikz@to@out@looseness=\pgfmathresult%
  \let\tikz@to@start@compute=\tikz@to@start@compute@looseness%
  \tikz@to@switch@on%
}


\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
    \draw (#2.south east) to [in=90, out=90, looseness=0.85+#1] (#3.south west);
}

\begin{document}    
\begin{tikzpicture}[start chain]
    \node[on chain] (k11) {};
    \node[on chain] (k12) {};
    \mypath[-0.5]{k11}{k12}       
\end{tikzpicture}    
\end{document}

答案2

另一种方法是用表达式填充宏。(因此不需要使选项数学可解析)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}


\newcommand{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
\pgfmathsetmacro{\mytemp}{.85+ #1}
    \draw (#2.south east) to [looseness=\mytemp,in=90, out=90, ] (#3.south west);
}

\begin{document}    
\begin{tikzpicture}[start chain]
    \node[on chain] (k11) {};
    \node[on chain] (k12) {};
    \mypath[1]{k11}{k12} 
    \mypath[0]{k11}{k12} 
\end{tikzpicture}    


\end{document}

答案3

解决同一基本问题(需要对值进行浮点运算)的另一种方法,这次使用 LaTeX3 的 FPU

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
    \draw (#2.south east) to [in=90, out=90, looseness=\fpeval{0.85+#1}] (#3.south west);
}

\begin{document}    
\begin{tikzpicture}[start chain]
    \node[on chain] (k11) {};
    \node[on chain] (k12) {};
    \mypath{k11}{k12}       
\end{tikzpicture}    
\end{document}

(LaTeX3 代码是可扩展的,因此可以简单地放入底层 TikZ 代码需要某种形式的数字的地方。)

答案4

使用fp包可以工作,如另一个问题

\documentclass{article}
\usepackage[nomessages]{fp}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,chains}

\newcommand*{\mypath}[3][0.0]{% looseness adjustment (optional),1st node, 2nd node
    \FPeval\loose{0.85+(#1)}
    \draw   (#2.south east) to [in=90, out=90, looseness=\loose] (#3.south west);
}

\begin{document}

\begin{tikzpicture}[start chain]

    \node[on chain] (k11) {};
    \node[on chain] (k12) {};

    \mypath[4.0]{k11}{k12}

\end{tikzpicture}

\end{document}

相关内容