我想重新定义 minipage 环境,它有几个可选参数。默认值renewenvironment
只允许一个可选参数。因此,我尝试使用xparse
套餐包含\RenewDocumentEnvironment
:
梅威瑟:
\documentclass{standalone}
\usepackage{xparse}
\RenewDocumentEnvironment{minipage}{O{} O{} O{} m}{\begin{minipage}[#1][#2][#3]{#4}}{\end{minipage}}
\begin{document}
\begin{minipage}{\linewidth}
hello world
\end{minipage}
\end{document}
但是,此文档无法编译成功:
! TeX capacity exceeded, sorry [grouping levels=255].
\__xparse_grab_arg:w ...e_args_tl }}\group_begin:
\token_if_eq_catcode:NNTF ...
l.5 \begin{minipage}{\linewidth}
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on demo-xparse-minipage.log.
答案1
您不能minipage
根据其自身重新定义;您还必须仔细解析可选参数,因为 LaTeX 无法遵守原始的空可选参数minipage
。
\documentclass[border=2pt]{standalone}
\usepackage{xparse}
% save the old commands
\let\latexminipage\minipage
\let\latexendminipage\endminipage
\RenewDocumentEnvironment{minipage}{ooom}
{%
\IfNoValueTF{#1}
{\latexminipage{#4}}
{%
\IfNoValueTF{#2}
{\latexminipage[#1]{#4}}
{%
\IfNoValueTF{#3}
{\latexminipage[#1][#2]{#4}}
{\latexminipage[#1][#2][#3]{#4}}%
}%
}%
}
{\latexendminipage}
\begin{document}
\begin{minipage}{3cm}
hello world
\end{minipage}
\begin{minipage}[b]{3cm}
hello world\\
hello world
\end{minipage}
\begin{minipage}[b][4cm]{3cm}
hello world\\
hello world
\end{minipage}
\begin{minipage}[b][4cm][s]{3cm}
hello world
\vfill
hello world
\end{minipage}
\end{document}