newenvironment%20%E5%8C%85%E5%90%AB%E7%92%B0%E5%A2%83%E6%9C%AC%E8%BA%AB%E4%BB%A5%E5%8F%8A%20LaTeX%20%E4%B8%AD%E7%9A%84%E5%8F%83%E6%95%B8.png)
如何使用 multicols 環境等參數重新定義環境?目標是定義兩組指令,每組指令都基於本機 multicols 環境:
\LetLtxMacro\oldmulticols\multicols
% first command
\newenvironment{mastermulticols}[1]
{ do some stuff \begin{\oldmulticols}{#1}
{\end{\oldmulticols} do some stuff}
% second command
\renewenvironment{multicols}[1]
{do some other stuff \begin{\oldmulticols}{#1}
{\end{oldmulticols} do some other stuff}
對我來說,可以使用第二個命令很重要,因為我的文件中\begin{multicols}{some number}
已經出現了很多。\begin{multicols}
因此,使用兩個不同的名稱創建兩個不同的環境\newenvironment{}
對我沒有幫助。
\LetLtxMacro\oldmulticols\multicols
我在答案中看到了使用命令的方法艾格雷格處理不需要額外參數的圖形環境。
我的問題是我不知道這樣做的確切語法。有人可以幫我解決這個問題嗎?
答案1
\LetLtxMacro
在這種情況下你不需要。相反,您不應該使用參數重新定義環境:只需將其傳遞給已儲存的巨集(除非您需要在環境開始時執行某些操作。
\documentclass{article}
\usepackage{multicol}
\usepackage{lipsum}
\let\FMmulticols\multicols
\let\endFMmulticols\endmulticols
\renewenvironment{multicols}
{<some stuff before>\FMmulticols}
{\endFMmulticols<some stuff after>}
\begin{document}
\begin{multicols}{2}[\section{A section title to show it works}]
\lipsum[1-2]
\end{multicols}
\end{document}
一種不同的(甚至更靈活的)策略是etoolbox
:
\documentclass{article}
\usepackage{multicol}
\usepackage{etoolbox}
\usepackage{lipsum}
\BeforeBeginEnvironment{multicols}{%
<some stuff before>%
}
\AfterEndEnvironment{multicols}{%
<some stuff after>%
}
\begin{document}
\begin{multicols}{2}[\section{A section title to show it works}]
\lipsum[1-2]
\end{multicols}
\end{document}
出於實現原因,實際上不可能根據 定義環境multicols
,因為這個環境明確檢查\end{multicols}
。
這是一種通用方法,可以與\BeforeBeginEnvironment
和\AfterEndEnvironment
方法結合使用,以向標準添加內容multicols
。
\documentclass{article}
\usepackage{multicol,etoolbox}
\usepackage{lipsum}
% keep copies of the original to be on the safe side
\let\FMmulticols\multicols
\let\endFMmulticols\endmulticols
\makeatletter
\newcommand{\newmulticolsenvironment}[3]{%
\newenvironment{#1}{#2\FMmulticols}{}%
\toks@=\expandafter{\endFMmulticols#3}%
\expandafter\edef\csname end#1\endcsname{\the\toks@}%
\expandafter\patchcmd\csname end#1\endcsname
{\@checkend{multicols}}
{\@checkend{#1}}
{}{}%
}
\newmulticolsenvironment{mastermulticols}
{<some stuff before>}
{<some stuff after>}
\begin{document}
\begin{mastermulticols}{2}[\section{A section title to show it works}]
\lipsum[1-2]
\end{mastermulticols}
\end{document}
*-版本的添加
透過一些擴展,我們也可以為我們定義的每個環境定義 *-版本\newmulticolsenvironment
:
\documentclass{article}
\usepackage{multicol,etoolbox}
\usepackage{lipsum}
% keep copies of the original to be on the safe side
\let\FMmulticols\multicols
\let\endFMmulticols\endmulticols
\letcs\FMmulticolsstar{multicols*}
\letcs\endFMmulticolsstar{endmulticols*}
\patchcmd{\FMmulticolsstar}{\begin{multicols}}{}{}{}
\patchcmd{\endFMmulticolsstar}{\end{multicols}}{}{}{}
\makeatletter
\newcommand{\newmulticolsenvironment}[3]{%
\newenvironment{#1}{#2\FMmulticols}{}%
\toks@=\expandafter{\endFMmulticols#3}%
\expandafter\edef\csname end#1\endcsname{\the\toks@}%
\expandafter\patchcmd\csname end#1\endcsname
{\@checkend{multicols}}
{\@checkend{#1}}
{}{}%
\newenvironment{#1*}
{#2\FMmulticolsstar\begin{#1}}
{\endFMmulticolsstar\end{#1}#3}%
}
\newmulticolsenvironment{mastermulticols}
{<some stuff before>}
{<some stuff after>}
\begin{document}
\begin{mastermulticols*}{2}[\section{A section title to show it works}]
\lipsum[1-4]
\end{mastermulticols*}
\end{document}