我正在制作一个\logo
类似于\title
用于标题页的命令。默认情况下,我希望使用graphbox
smash
key-val 参数tl
。但稍后(取决于用户的选择,例如通过类选项)我希望能够将其更改为(bl
例如)。
请参阅下面的 MWE
\documentclass[11pt]{scrartcl}
\usepackage{graphicx}
\usepackage{graphbox}
\makeatletter
%%% this works, but I would like to change the default "smash=tl" value later on
%\NewDocumentCommand{\logo}{m O{4.0cm} O{tl} }{%
% \gdef\@logo{\includegraphics[width=#2,smash=tl]{#1}}
%}
%%%%% \newcommand{\TitleLogoPlaceDefault}{tl}
%%% this throws secondoftwo argument has an extra } error
\gdef\TitleLogoPlaceDefault{tl}
\NewDocumentCommand{\logo}{m O{4.0cm} O{\TitleLogoPlaceDefault} }{%
\gdef\@logo{\includegraphics[width=#2,smash=#3]{#1}}
}
% this also doesnt work, keval smash=tl undefined
%\gdef\TitleLogoPlaceDefault{smash=tl}
%\NewDocumentCommand{\logo}{m O{4.0cm} O{\TitleLogoPlaceDefault} }{%
% \gdef\@logo{\includegraphics[width=#2,#3,]{#1}}
%}
\makeatother
%%% class option should allow one to change to \gdef\TitleLogoPlaceDefault{bl}
\begin{document}
\logo{example-image-b}
\makeatletter
\@logo
\makeatother
\end{document}
答案1
您需要先扩展\TitleLogoPlaceDefault
它,然后再将其转发给smash
密钥。如果您正在使用,\NewDocumentCommand
则可以使用参数处理器来执行此操作,该处理器将扩展参数中的第一个标记一次。
但我不会对\logo
宏中可用的键进行硬编码,这意味着我会使用略有不同的实现。此外,以下两个可选参数会导致界面不佳(如何在不使用第一个可选参数的情况下使用第二个可选参数?)。
\documentclass[11pt]{scrartcl}
\usepackage{graphicx}
\usepackage{graphbox}
\makeatletter
%%%%% \newcommand{\TitleLogoPlaceDefault}{tl}
%%% this throws secondoftwo argument has an extra } error
\gdef\TitleLogoPlaceDefault{tl}
\NewDocumentCommand{\logo}{O{width=4cm} m >{\ExpArgOnceProc}O{\TitleLogoPlaceDefault} }{%
\gdef\@logo{\includegraphics[#1,smash=#3]{#2}}%
}
\newcommand\ExpArgOnceProc[1]
{\edef\ProcessedArgument{\unexpanded\expandafter{#1}}}
\makeatother
%%% class option should allow one to change to \gdef\TitleLogoPlaceDefault{bl}
\begin{document}
\logo{example-image-b}
\makeatletter
\@logo
\makeatother
\end{document}
更强大的版本,仅在确实需要默认值时才扩展第一个标记:
\documentclass[11pt]{scrartcl}
\usepackage{graphicx}
\usepackage{graphbox}
\makeatletter
%%%%% \newcommand{\TitleLogoPlaceDefault}{tl}
%%% this throws secondoftwo argument has an extra } error
\gdef\TitleLogoPlaceDefault{tl}
\ExplSyntaxOn
\cs_new_protected:Npn \__likethevegetable_logo:nnn #1#2#3
{
\cs_gset:cpn { @logo } { \includegraphics[{#1, smash=#3}]{#2} }
}
\NewDocumentCommand\logo { O{with=4cm} m o }
{%
\IfNoValueTF {#3}
{
\exp_args:NnnV
\__likethevegetable_logo:nnn {#1} {#2} \TitleLogoPlaceDefault
}
{ \__likethevegetable_logo:nnn {#1} {#2} {#3} }
}
\ExplSyntaxOff
\makeatother
%%% class option should allow one to change to \gdef\TitleLogoPlaceDefault{bl}
\begin{document}
\logo{example-image-b}
\makeatletter
\@logo
\makeatother
\end{document}