我想打开一个环境,将宏的内容传递给可选参数。到目前为止,我尝试了使用 \expandafter 的不同方法,但我无法获得预期的行为。
\RequirePackage{graphicx}
\RequirePackage{xkeyval}
% In my original code I've got some key definition
% for [myStyle]{floating} at this place, I guess the real
% definition does not matter but would distract.
% Place an image within a floating environment and add caption
% and label automatically
\newcommand{\img}[2][]{
\setkeys*{Gin}{#1}
\setkeys[myStyle]{floating}{\XKV@rm}
\begin{figure}[\XKV@myStyle@floating@placement@value]
\includegraphics{#2}
\caption{\XKV@myStyle@floating@caption@value}
\label{fig:\XKV@myStyle@floating@label@value}
\end{figure}
}
现在,当我尝试使用 \img 命令时,如下:
\img[caption = A picture, label = TheLabel, placement = h!]{mm-01-01}
图片已插入,它有标签和标题,但放置选项似乎被忽略了。
为什么?如何在不定义新环境的情况下解决这个问题?
完整代码如下:
\documentclass[
paper = a4,
fontsize = 11pt,
index = totoc,
listof = totoc,
parskip
]{scrartcl}
\usepackage{xkeyval}
\usepackage{graphicx}
\usepackage{lipsum}
\makeatletter
% \parameter caption The caption of the item within the floating environment.
% If caption is empty, no caption will be placed.
\define@key[myStyle]{floating}{caption}[]{
\PackageInfo{myStyle}{Setting caption to "#1"}
}
% \parameter label The label of the item within the floating environment.
% If label is empty, no label will be placed.
\define@key[myStyle]{floating}{label}[]{
\PackageInfo{myStyle}{Setting label to "#1"}
}
% \parameter placement Can be used in the same way as the optional placement
% argumants of floating environments
\define@key[myStyle]{floating}{placement}[]{
\PackageInfo{myStyle}{Setting placement to "#1"}
}
% Setting default values.
\savekeys[myStyle]{floating}{caption,label,placement}
\presetkeys[myStyle]{floating}{caption, label, placement=htbp}{}
\newcommand{\img}[2][]{
\setkeys*{Gin}{#1}
\setrmkeys[myStyle]{floating}
\begin{figure}
\centering
\includegraphics{#2}
\caption{\XKV@myStyle@floating@caption@value}
\label{fig:\XKV@myStyle@floating@label@value}
\end{figure}
}
\makeatother
\begin{document}
\lipsum[2]
The picture \ref{fig:APicture} shall be located exactly after this line,
however it goes on the top of the page
\img[
label = APicture,
caption = Witwe Bolte,
placement = h!,
width = 0.3\textwidth
]{mm-01-01}
\lipsum[3]
\end{document}
答案1
对于图你可以使用
\begin{figure}
没有选项,但在此之前设置默认值:
\let\fps@figure\XKV@myStyle@floating@placement@value
一般来说你可以
\def\zz{\begin{figure}[}
\expandafter\zz\XKV@myStyle@floating@placement@value]
答案2
没有\XKV@myStyle@floating@placement@value
定义你什么时候做
\define@cmdkey[myStyle]{floating}{placement}{}
存储值的宏是\cmdmyStyle@floating@placement
。因此,您可能想执行以下操作:
\documentclass{article}
\usepackage{xkeyval}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\makeatletter
\define@cmdkey[myStyle]{floating}{caption}[]{\PackageInfo{myStyle}{Setting caption to "#1"}}
\define@cmdkey[myStyle]{floating}{label}[]{\PackageInfo{myStyle}{Setting label to "#1"}}
\define@cmdkey[myStyle]{floating}{placement}[]{\PackageInfo{myStyle}{Setting caption to "#1"}}
\newcommand{\img}[2][]{%
\begingroup % don't clutter the key values for the following \img commands
\setkeys*{Gin}{#1}%
\setrmkeys[myStyle]{floating}%
\begingroup\edef\x{\endgroup\noexpand\begin{figure}[\cmdmyStyle@floating@placement]}\x
\includegraphics{#2}
\caption{\cmdmyStyle@floating@caption}
\label{fig:\cmdmyStyle@floating@label}
\end{figure}
\endgroup
}
% Setting default values.
\savekeys[myStyle]{floating}{caption,label,placement}
\presetkeys[myStyle]{floating}{caption, label, placement=htbp}{}
\makeatother
\begin{document}
First the references: \ref{fig:TheLabel1} and \ref{fig:TheLabel2}
\lipsum[1]
\img[
caption = A picture,
label = TheLabel1,
placement = h!,
width=\textwidth,
]{mm-01-01}
\lipsum[2-4]
\img[
caption = A picture,
label = TheLabel2,
placement = t,
width=\textwidth,
]{mm-01-01}
\lipsum[3]
\end{document}