这类问题总是可以通过把必要的信息写入到辅助文件中解决。
\documentclass{article}
\usepackage[many]{tcolorbox}
\newcounter{problem}
\newcounter{solution}
\makeatletter
\protected\def\solutionwriteaux{\immediate\write\@auxout{%
\global\string\@namedef
{solution@count@of-\number\value{problem}}{\number\value{solution}}}}
\def\solutionmaxcount{\@ifundefined{solution@count@of-\number\value{problem}}
{1}{\@nameuse{solution@count@of-\number\value{problem}}}}
\def\thesolutionornone#1{\ifnum\number\solutionmaxcount>\@ne #1\thesolution\fi} % #1: sep
\makeatletter
\newcommand*{\Problem}[1]{%
\setcounter{solution}{0}
\refstepcounter{problem}%
\begin{tcolorbox}[enhanced,title={\bfseries Problem~\theproblem},colframe=magenta!80!white,colback=magenta!20!white]
#1
\end{tcolorbox}
}
\newcommand*{\Solution}[1]{%
\refstepcounter{solution}%
\solutionwriteaux % <- 这里写入辅助文件
\begin{tcolorbox}[enhanced,
title={\bfseries Solution~\theproblem \thesolutionornone{-}}, % <- 检查是否多于一个
colframe=cyan!80!white,colback=cyan!20!white]
#1
\end{tcolorbox}
}
\begin{document}
\Problem{This is the problem statement.}
\Solution{This is the first solution of the problem}
xi
\Problem{This is another problem but two solution multiple line.}
\Solution{This is the second solution.}
\Solution{This is the second solution.}
\Problem{This is another problem balabalabala}
\Solution{This is the only solution}
\Problem{This is another problem balabalabala}
\end{document}
问 TeX是否有办法在顺序的执行次序下获取之后文本的信息