novo%20ambiente%20que%20cont%C3%A9m%20o%20pr%C3%B3prio%20ambiente%20com%20um%20argumento%20em%20LaTeX.png)
Como posso redefinir um ambiente com um argumento como o ambiente multicols? O objetivo é definir dois conjuntos de comandos, cada um deles baseado no ambiente multicols nativo:
\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}
É importante para mim poder usar o segundo comando \begin{multicols}{some number}
porque já existem muitas ocorrências de \begin{multicols}
no meu documento. Portanto, criar dois ambientes diferentes com \newenvironment{}
dois nomes diferentes não é útil para mim.
Eu vi a abordagem com o \LetLtxMacro\oldmulticols\multicols
comando em uma resposta deegrégialidar com o ambiente da figura que não requer argumentos adicionais.
Meu problema é que não sei a sintaxe exata para fazer isso. Alguém pode me ajudar com esse problema?
Responder1
Você não precisa \LetLtxMacro
neste caso. Em vez disso, você não deve redefinir o ambiente com um argumento: apenas passe-o para a macro salva (a menos que precise fazer algo logo no início do ambiente.
\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}
Uma estratégia diferente (e ainda mais flexível) é 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}
Por razões de implementação, não é realmente possível definir um ambiente em termos de multicols
, porque este verifica explicitamente \end{multicols}
.
Aqui está uma abordagem genérica que pode ser combinada com a abordagem \BeforeBeginEnvironment
e \AfterEndEnvironment
para adicionar coisas ao padrão 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}
Adição para a versão *
Com alguma extensão, também podemos definir a versão * para cada ambiente que definimos com \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}