如何在 \newcommand 中向 \textcite 传递可选参数?

如何在 \newcommand 中向 \textcite 传递可选参数?

我想定义一个新命令\mycite,它调用\textcite\mycite获取一个可选参数。 如果提供,该参数应作为 的可选参数 ( [])出现\textcite。 以下 MWE 显示\mycite没有参数已经可以工作,但\mycite{Section~10}与 不同\textcite[Section~10]{...}xparse可能是一个解决方案,但也许下面的可以轻松修复 (?)

\documentclass{scrartcl}
\usepackage[style=authoryear]{biblatex}
\usepackage{filecontents}
\begin{filecontents*}{bib.bib}
@Article{mykey,
   author = {TeX, L.},
   journal = {Journal of LaTeX questions},
   pages = {1--10},
   title = {Minimal title},
   volume = 1,
   number = 1,
   year = 2011
}
\end{filecontents*}
\addbibresource{bib.bib}
\newcommand*{\mycite}[1][]{\textcite[#1]{mykey}}
\begin{document}
\mycite % without argument; works

\mycite{Section~10} % should behave like the below

\textcite[Section~10]{mykey} % wanted behavior if \mycite was provided with an argument
\printbibliography
\end{document}

答案1

\mycite[Section~10] % Optional arguments always in square brackets

或定义

\makeatletter
\def\mycite{\@ifnextchar\bgroup{\my@cite}{\my@cite{}}}
\def\my@cite#1{\textcite[#1]{mykey}}
\makeatother

如果你想使用\mycite{Section~10}

相关内容