t***r 发帖数: 4 | 1 我想定义一个newcommand,有两个参数,第一个optinal, 第二个必须有
问题是,我想实现:如果没有提供第一个参数,他的值就是第二个参数
but it did not work if I say:
\newcommand[2][#2]{....}
It looks #2 does not work here.
Any idea? Thanks a lot! |
h*****0 发帖数: 4889 | 2 我有个麻烦点的方法,自己弄的,能凑合:
\newcommand{\default}{default}
\newcommand{\cs}[2][\default]{\renewcommand{\default}{#2} first={#1}, second
={#2}}
【在 t***r 的大作中提到】 : 我想定义一个newcommand,有两个参数,第一个optinal, 第二个必须有 : 问题是,我想实现:如果没有提供第一个参数,他的值就是第二个参数 : but it did not work if I say: : \newcommand[2][#2]{....} : It looks #2 does not work here. : Any idea? Thanks a lot!
|
t***r 发帖数: 4 | 3 I will try your solution. Thanks a lot!
second
【在 h*****0 的大作中提到】 : 我有个麻烦点的方法,自己弄的,能凑合: : \newcommand{\default}{default} : \newcommand{\cs}[2][\default]{\renewcommand{\default}{#2} first={#1}, second : ={#2}}
|
T*******n 发帖数: 493 | 4 \documentclass{minimal}
\newcommand*{\mywrappercommand}[2][\undefined]{%
\ifx#1\undefined
\myactualcommand{#2}{#2}%
\else
\myactualcommand{#1}{#2}%
\fi
}
\newcommand*{\myactualcommand}[2]{%
Optional argument = #1; mandatory argument = #2.\par
}
\begin{document}
\verb|\mywrappercommand{abc}:| \mywrappercommand{abc}
\verb|\mywrappercommand[xyz]{abc}:| \mywrappercommand[xyz]{abc}
\end{document}
【在 t***r 的大作中提到】 : 我想定义一个newcommand,有两个参数,第一个optinal, 第二个必须有 : 问题是,我想实现:如果没有提供第一个参数,他的值就是第二个参数 : but it did not work if I say: : \newcommand[2][#2]{....} : It looks #2 does not work here. : Any idea? Thanks a lot!
|
h*****0 发帖数: 4889 | 5 \undefined是自己定义的还是原来就有的?
似乎好像比我的还要麻烦一点……
【在 T*******n 的大作中提到】 : \documentclass{minimal} : \newcommand*{\mywrappercommand}[2][\undefined]{% : \ifx#1\undefined : \myactualcommand{#2}{#2}% : \else : \myactualcommand{#1}{#2}% : \fi : } : \newcommand*{\myactualcommand}[2]{% : Optional argument = #1; mandatory argument = #2.\par
|
T*******n 发帖数: 493 | 6 \undefined is never defined anywhere!
Your method works too, but it relies on the use of a global \default,
which could be prone to side effects. You could make it more robust
by changing \default to have private scope:
\makeatletter
\newcommand{\CS}[2][\@default]{%
\begingroup
\newcommand{\@default}{}%
\renewcommand{\@default}{#2}%
first={#1}, second ={#2}
\endgroup
}
\makeatother
【在 h*****0 的大作中提到】 : \undefined是自己定义的还是原来就有的? : 似乎好像比我的还要麻烦一点……
|
h*****0 发帖数: 4889 | 7 nice!
我原本是想找个“局部”变量的,但是不会。这些东西从哪可以学到?
【在 T*******n 的大作中提到】 : \undefined is never defined anywhere! : Your method works too, but it relies on the use of a global \default, : which could be prone to side effects. You could make it more robust : by changing \default to have private scope: : \makeatletter : \newcommand{\CS}[2][\@default]{% : \begingroup : \newcommand{\@default}{}% : \renewcommand{\@default}{#2}% : first={#1}, second ={#2}
|
h*****0 发帖数: 4889 | 8 \undefined是一个“值”对吗?
【在 T*******n 的大作中提到】 : \undefined is never defined anywhere! : Your method works too, but it relies on the use of a global \default, : which could be prone to side effects. You could make it more robust : by changing \default to have private scope: : \makeatletter : \newcommand{\CS}[2][\@default]{% : \begingroup : \newcommand{\@default}{}% : \renewcommand{\@default}{#2}% : first={#1}, second ={#2}
|
T*******n 发帖数: 493 | 9 It has no value, but it has special properties as explained in the TeXbook.
【在 h*****0 的大作中提到】 : \undefined是一个“值”对吗?
|