![我怎样才能避免复制和粘贴此代码(使用不同功能的类似新命令定义):](https://linux22.com/image/399762/%E6%88%91%E6%80%8E%E6%A0%B7%E6%89%8D%E8%83%BD%E9%81%BF%E5%85%8D%E5%A4%8D%E5%88%B6%E5%92%8C%E7%B2%98%E8%B4%B4%E6%AD%A4%E4%BB%A3%E7%A0%81%EF%BC%88%E4%BD%BF%E7%94%A8%E4%B8%8D%E5%90%8C%E5%8A%9F%E8%83%BD%E7%9A%84%E7%B1%BB%E4%BC%BC%E6%96%B0%E5%91%BD%E4%BB%A4%E5%AE%9A%E4%B9%89%EF%BC%89%EF%BC%9A.png)
感谢你们,我一直在改进我的代码,但这看起来仍然可以改进,因为它看起来像一个丑陋的复制/粘贴:
\newcommand*{\LegendFlecheInf}[5]{
\coordinate (C2) at (axis cs:#2, { \FInfAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheEga}[5]{
\coordinate (C2) at (axis cs:#2, { \FEgaAUn(#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheSup}[5]{
\coordinate (C2) at (axis cs:#2, { \FSupAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
这里定义的 3 个命令之间的唯一区别在于将调用哪个函数来计算箭头末端的 y 坐标。
我已经为 MWE 删除了很多内容但它仍然很大:
\documentclass[border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{tikz,pgfplots}
\usetikzlibrary{math} % Pour evaluate
\usetikzlibrary{calc} % Pour postionnement relatif
\def\FInfAUn(#1,#2){( 1 - ( (exp(-W*#1*#2)/sqrt(1-#1*#1)) *cos((W*#2*sqrt(1-#1*#1)
- (atan(#1/sqrt(1-#1*#1))*pi/180) ) *180/pi) ) ) *\Kg }
\def\FTUn(#1){( 1/( W*(#1-sqrt((#1*#1) -1))) )}
\def\FTDeux(#1){( 1/(W *(#1+sqrt((#1*#1) -1))) )}
\def\FInterSupAUn(#1,#2,#3){( \Kg * (1 - ( 1/( #1 - #2 ) )*( #1 *exp(-#3/ #1 ) - #2 *exp(-#3/ #2 ) )) )}
\def\FSupAUn(#1,#2){ \FInterSupAUn({\FTUn(#1)},{\FTDeux(#1)},#2) }
\def\FEgaAUn(#1){ \Kg * (1 - ( 1+ #1/(1/W) )*exp(-#1/(1/W)) ) }
\newcommand*{\LegendFlecheInf}[5]{
\coordinate (C2) at (axis cs:#2, { \FInfAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheEga}[5]{
\coordinate (C2) at (axis cs:#2, { \FEgaAUn(#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheSup}[5]{
\coordinate (C2) at (axis cs:#2, { \FSupAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\tikzset{ fleche/.style={<-,>=latex,line width=0.2mm,shorten <=-0.02cm} }
\begin{document}
\begin{tikzpicture}[
declare function={
W=pi*2;
}]
\def\Kg{2}
\def\Ezero{1}
\def\Ttrace{3}
\begin{axis}[ymax=1.55*\Kg]
\addplot[blue,loosely dashed,thick=3pt,ultra thick,domain=0:3] { \FEgaAUn(x) }; \label{d}
%\LegendFleche{z}{t}{Anchor of new node}{New node name}{previous node anchor coinsiding with this anchor}
\LegendFlecheInf{0.2}{0.6}{west}{C3}{($(C2)+(axis cs:0.5,0.05)$)}
\LegendFlecheSup{4}{0.6}{west}{C3}{($(C2)+(axis cs:1,-0.5)$)}
\LegendFlecheSup{2}{0.6}{south}{C4}{(C3.north)}
\LegendFlecheEga{1}{0.6}{south}{C5}{(C4.north)}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
你可以更换
\newcommand*{\LegendFlecheInf}[5]{
\coordinate (C2) at (axis cs:#2, { \FInfAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheEga}[5]{
\coordinate (C2) at (axis cs:#2, { \FEgaAUn(#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
\newcommand*{\LegendFlecheSup}[5]{
\coordinate (C2) at (axis cs:#2, { \FSupAUn(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
经过
\newcommand*{\Legendx}[6]{
\coordinate (C2) at (axis cs:#2, { #6(#1,#2) }) ;
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
然后替换例如
\LegendFlecheEga{1}{0.6}{south}{C5}{(C4.north)}
经过
\Legendx{1}{0.6}{south}{C5}{(C4.north)}\FEgaAUn
答案2
利用该ifthen
软件包,您可以用一个新命令替换这三个命令:
\newcommand{\LegendComplex}[6]{
\ifthenelse{\equal{#6}{inf}}{\coordinate (C2) at (axis cs:#2, {\FInfAUn(#1,#2)});}{}
\ifthenelse{\equal{#6}{sup}}{\coordinate (C2) at (axis cs:#2, {\FSupAUn(#1,#2)});}{}
\ifthenelse{\equal{#6}{ega}}{\coordinate (C2) at (axis cs:#2, {\FEgaAUn(#2)});}{}
\draw node[anchor=#3] (#4) at #5 {$z =#1$};
\draw[fleche] (C2) -- (#4.west) ;
}
您必须提供一个新参数(sup、inf 或 ega)来指定您想要调用哪个函数。