是否可以创建环境或类似的东西来获得行为:
示例输入
\section
\begin{problem}
problem 1
\begin{solution}{section 1}
solution 1
\end{solution}
\end{problem}
\begin{problem}
problem 2
\begin{solution}{section 1}
solution 2
\end{solution}
\end{problem}
\begin{problem}
problem 3
\begin{solution}{section 2}
solution 3
\end{solution}
\end{problem}
期望输出:
problem 1
problem 2
problem 3
section 1
solution 1
solution 2
section 2
solution 3
答案1
我可以想到这样的事情:
\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem*{printsolution}{Problem \problemnumber}
\ExplSyntaxOn
\NewDocumentEnvironment{solution}{+b}
{
\angelos_solutions_grab:n { #1 }
}
{}
\NewDocumentCommand{\printsolutions}{}
{
\angelos_solutions_print:
}
\seq_new:N \g_angelos_solutions_seq
\int_new:N \l_angelos_solutions_section_int
\cs_new_protected:Nn \angelos_solutions_grab:n
{
\seq_gput_right:Nx \g_angelos_solutions_seq
{
\angelos_solutions_item:nnn
{ \int_to_arabic:n { \value{problem} } }
{ \int_to_arabic:n { \value{section} } }
{ \exp_not:n { #1 } }
}
}
\cs_new_protected:Nn \angelos_solutions_item:nnn
{
\int_compare:nF { #2 = \l_angelos_solutions_section_int }
{
\subsection*{Section~#2}
\int_set:Nn \l_angelos_solutions_section_int { #2 }
}
\cs_set:Npn \problemnumber { #1 }
\begin{printsolution}
#3
\end{printsolution}
}
\cs_new_protected:Nn \angelos_solutions_print:
{
\section*{Solutions}
\seq_map_function:NN \g_angelos_solutions_seq \use:n
}
\ExplSyntaxOff
\begin{document}
\section{First}
\begin{problem}
First problem in the first section
\begin{solution}
Solution to the first problem in the first section
\end{solution}
\end{problem}
\begin{problem}
Second problem in the first section
\begin{solution}
Solution to the second problem in the first section
\end{solution}
\end{problem}
\section{Second}
This section has no problems
\section{Third}
\begin{problem}
First problem in the third section
\begin{solution}
Solution to the first problem in the third section
\end{solution}
\end{problem}
\printsolutions
\end{document}
解决方案与当前问题编号和当前章节编号一起按序列存储。
命令\printsolutions
传递内容,必要时发出\subsection*
命令。
nameref
如果您想重复章节标题,可以使用。
\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}
\usepackage{nameref}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem*{printsolution}{Problem \problemnumber}
\ExplSyntaxOn
\cs_set_eq:NN \angelos_section: \section
\cs_new_protected:Nn \angelos_nameref:n { \nameref { #1 } }
\RenewDocumentCommand{\section}{sO{#3}m}
{
\IfBooleanTF { #1 }
{
\angelos_section: * { #3 }
}
{
\angelos_section: [#2]{#3} \label{angelosautolabel\int_to_roman:n { \value{section} } }
}
}
\NewDocumentEnvironment{solution}{+b}
{
\angelos_solutions_grab:n { #1 }
}
{}
\NewDocumentCommand{\printsolutions}{}
{
\angelos_solutions_print:
}
\seq_new:N \g_angelos_solutions_seq
\int_new:N \l_angelos_solutions_section_int
\cs_new_protected:Nn \angelos_solutions_grab:n
{
\seq_gput_right:Nx \g_angelos_solutions_seq
{
\angelos_solutions_item:nnnn
{ \int_to_arabic:n { \value{problem} } }
{ \int_to_arabic:n { \value{section} } }
{ \exp_not:N \protect \exp_not:N \nameref {angelosautolabel\int_to_roman:n { \value{section} } } }
{ \exp_not:n { #1 } }
}
}
\cs_new_protected:Nn \angelos_solutions_item:nnnn
{
\int_compare:nF { #2 = \l_angelos_solutions_section_int }
{
\subsection*{Section~#2\ --\ #3}
\int_set:Nn \l_angelos_solutions_section_int { #2 }
}
\cs_set:Npn \problemnumber { #1 }
\begin{printsolution}
#4
\end{printsolution}
}
\cs_new_protected:Nn \angelos_solutions_print:
{
\section*{Solutions}
\seq_use:Nn \g_angelos_solutions_seq {}
}
\ExplSyntaxOff
\begin{document}
\section{First}
\begin{problem}
First problem in the first section
\begin{solution}
Solution to the first problem in the first section
\end{solution}
\end{problem}
\begin{problem}
Second problem in the first section
\begin{solution}
Solution to the second problem in the first section
\end{solution}
\end{problem}
\section{Second}
This section has no problems
\section{Third}
\begin{problem}
First problem in the third section
\begin{solution}
Solution to the first problem in the third section
\end{solution}
\end{problem}
\printsolutions
\end{document}