我是编写多选项宏的新手。我想要做的是将多个选项组合到一对括号中。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\def\DLineS{%
\@ifnextchar[%
{\DLineS@i}
{\DLineS@i[1.25]}%
}
\def\DLineS@i[#1]{%
\@ifnextchar[%
{\DLineS@ii{#1}}
{\DLineS@ii{#1}[-0.25]}%
}
\def\DLineS@ii#1[#2]#3#4{%
\draw[latex-latex] ($(#3)!#2!(#4)$) -- ($(#3)!#1!(#4)$)
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate[label=below:{\small $A$}] (A) at (0,0);
\coordinate[label=below:{\small $B$}] (B) at (1,1);
\filldraw (A) circle (1.5pt);
\filldraw (B) circle (1.5pt);
\DLineS[1.5][-0.75]{A}{B};
% I want this line is \DLineS[1.5,-0.75]{A}{B};
\end{tikzpicture}
\end{document}
答案1
除非您知道自己在做什么,否则不建议您使用\def
。如果是这样,您可以执行以下操作。否则,请考虑使用 egreg 的答案xparse
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\def\DLineS{\@ifnextchar[%
{\DLineS@i}{\DLineS@i[1.25]}}
\def\DLineS@i[#1]{\@ifnextchar[%
{\DLineS@ii{#1}}{\DLineS@iii#1,;}}
\def\DLineS@ii#1[#2]#3#4{%
\draw[latex-latex]($(#3)!#2!(#4)$)--($(#3)!#1!(#4)$)}
\def\DLineS@iii#1,#2;#3#4{\def\temp{#2}\ifx\temp\empty%
\DLineS@ii#1[-.25]{#3}{#4}\else\DLineS@iv[#1][#2]{#3}{#4}\fi}
\def\DLineS@iv[#1][#2,]{\DLineS@ii#1[#2]}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate[label=below:{\small $A$}](A)at(0,0);
\coordinate[label=below:{\small $B$}](B)at(1,1);
\coordinate[label=below:{\small $C$}](C)at(2,0);
\coordinate[label=below:{\small $D$}](D)at(3,1);
\coordinate[label=below:{\small $E$}](E)at(4,0);
\coordinate[label=below:{\small $F$}](F)at(5,1);
\coordinate[label=below:{\small $G$}](G)at(6,0);
\coordinate[label=below:{\small $H$}](H)at(7,1);
\filldraw(A)circle(1.5pt)(B)circle(1.5pt)(C)circle(1.5pt)(D)circle(1.5pt)(E)circle(1.5pt)(F)circle(1.5pt)(G)circle(1.5pt)(H)circle(1.5pt);
\DLineS[1.5][-0.75]{A}{B};
\DLineS[1.5,-0.75]{C}{D};
\DLineS[1.5]{E}{F};
\DLineS{G}{H};
\end{tikzpicture}
\end{document}
答案2
你可以这样做xparse
:
\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\NewDocumentCommand{\DLineS}{>{\SplitArgument{1}{,}}O{1.25,-0.25}mm}{%
% no optional argument is like calling [1.25,-0.25]
\DLineSaux#1{#2}{#3}%
}
\NewDocumentCommand{\DLineSaux}{mmmm}{%
\IfNoValueTF{#2}%
{\DLineSaux{#1}{-0.25}{#3}{#4}}% no comma in the optional argument, use -0.25
{\draw[latex-latex] ($(#3)!#2!(#4)$) -- ($(#3)!#1!(#4)$)}%
}
\begin{document}
\begin{tikzpicture}
\coordinate[label=below:{\small $A$}] (A) at (0,0);
\coordinate[label=below:{\small $B$}] (B) at (1,1);
\filldraw (A) circle (1.5pt);
\filldraw (B) circle (1.5pt);
\DLineS[1.5,-0.75]{A}{B};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate[label=below:{\small $A$}] (A) at (0,0);
\coordinate[label=below:{\small $B$}] (B) at (1,1);
\filldraw (A) circle (1.5pt);
\filldraw (B) circle (1.5pt);
\DLineS{A}{B};
\end{tikzpicture}
\begin{tikzpicture}
\coordinate[label=below:{\small $A$}] (A) at (0,0);
\coordinate[label=below:{\small $B$}] (B) at (1,1);
\filldraw (A) circle (1.5pt);
\filldraw (B) circle (1.5pt);
\DLineS[1.5]{A}{B};
\end{tikzpicture}
\end{document}
我们分为三个步骤;第一步,我们\DLineSaux
在逗号处拆分可选参数后将控制权传递给;如果没有给出可选参数,则[1.25,-0.25]
传递;如果只有第一部分(参见第三个示例),则xparse
传递-NoValue-
到下一个阶段,因此在这种情况下我们只需将其作为第二个参数\DLineSaux
来调用。-0.25