有没有办法将通用环境的 \begin 和 \end 命令之间的文本存储在数组中?
显然不起作用但显示了我的意思的代码如下
\documentclass{memoir}
\usepackage{arrayjobx}
\newarray\entry
\newnvironment{mycenter}{\begin{center}\entry{0}={}{}\end{center}} % \entry{0} should be the text between \begin{mycenter} and \end{mycenter}
\begin{document}
\begin{mycenter}
This line is centered.
\end{mycenter}
\entry{0} % \entry{0} should be 'This line is centered'
\end{document}
mycenter
这里我使用了基于的环境center
,但是具体环境无所谓,我只是用它作为一个例子。
编辑:我尝试environ
按照 David Carlisle 的建议使用该包,但我认为这里遗漏了一些东西。
编辑后的代码是
\documentclass{memoir}
\usepackage{arrayjobx, environ}
\newarray\entry
\NewEnviron{mycenter}{\entry(0)={\BODY}\begin{center}\BODY\end{center}}
\begin{document}
\begin{mycenter}
This line is centered.
\end{mycenter}
\entry(0)
\end{document}
仍然无法编译。我做错了什么?
答案1
绝对不是一项简单的任务。
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newtheoremx}{momo}
{
% * Let's duplicate the working of \newtheorem; \newtheoremx should
% be used for theorems that need to be listed
% * \newtheoremx{theorem}{Theorem} will actually do
% \newtheorem{theorem@inner}{Theorem} (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
\IfValueTF{#2}
{
\newtheorem{#1@inner}[#2@inner]{#3}
% #1 is subordinate to #2
\prop_gput:Nnn \g_riccardo_theorems_prop { #1 } { #2 }
}
{
\IfValueTF{#4}
{
\newtheorem{#1@inner}{#3}[#4]
}
{
\newtheorem{#1@inner}{#3}
}
% #1 is not subordinate, store the name itself
\prop_gput:Nnn \g_riccardo_theorems_prop { #1 } { #1 }
}
% define a "grabbing" environment #1 with the usual features
\NewEnviron{#1}[1][]
{
% start the inner environment (without or with optional argument)
\tl_if_empty:nTF { ##1 }
{ \begin{#1@inner} }
{ \begin{#1@inner}[##1] }
% save the statement number
\tl_gset:Nx \g__riccardo_theorems_number_tl { \use:c {@currentlabel} }
% typeset the statement
\BODY
% end the inner environment
\end{#1@inner}
% store the statement in a sequence variable, actually as
% four arguments as shown
\seq_gput_right:Nx \g_riccardo_theorems_seq
{
{ #1 } % name
{ \g__riccardo_theorems_number_tl } % number
{ \exp_not:n { ##1 } } % attribution
{ \exp_not:V \BODY } } % body
}
}
% allocate the needed variables
\prop_new:N \g_riccardo_theorems_prop
\seq_new:N \g_riccardo_theorems_seq
\tl_new:N \g__riccardo_theorems_number_tl
% print the stored theorems
\NewDocumentCommand{\printtheorems}{}
{
% we need a group where nullifying the action of \label
\group_begin:
\cs_set_eq:NN \label \use_none:n
% map the sequence, passing each item to the function that prints a theorem
\seq_map_function:NN \g_riccardo_theorems_seq \riccardo_printtheorems:n
% end the group
\group_end:
}
\cs_new_protected:Nn \riccardo_printtheorems:n
{
% just pass the argument in the form {name}{number}{attribution}{text}
% to a four argument function
\__riccardo_printtheorems:nnnn #1
}
\cs_new_protected:Nn \__riccardo_printtheorems:nnnn
{
% redefine \the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
\cs_set:cpn { the \prop_item:Nn \g_riccardo_theorems_prop {#1} @inner } { #2 }
\tl_if_empty:nTF { #3 }
{
% no attribution
\begin{#1@inner} #4 \end{#1@inner}
}
{
% attribution
\begin{#1@inner}[#3] #4 \end{#1@inner}
}
}
\ExplSyntaxOff
\newtheoremx{theorem}{Theorem}[section]
\newtheoremx{lemma}[theorem]{Lemma}
\begin{document}
\section{First test}
\begin{theorem}[Important]\label{thm:important}
This is a theorem about $\log_a x$.
\end{theorem}
\begin{lemma}\label{lem:whatever}
This is a lemma.
\end{lemma}
\begin{theorem}\label{thm:unimportant}
This is another theorem
\end{theorem}
\section{Second test}
\begin{theorem}\label{thm:soandso}
Again a theorem.
\end{theorem}
\section{Theorems}
\printtheorems
\end{document}