如何让LaTeX中的Solution环境实现跨页断行
解决自定义Question/Solution环境无法跨页的问题
你的核心问题在于minipage是不可断页的盒子,长内容会被强制限制在单页内,要么导致前页留白,要么被整体移到新页。要保留原有的边距效果同时支持跨页,推荐用changepage包的adjustwidth环境替代,它能灵活调整左右边距且允许内容自动断页。
修改后的环境代码
先引入changepage包,再重定义两个环境:
% 引入用于调整边距的changepage包 \usepackage{changepage} % 重定义question环境 \newenvironment{question}{ \setlength{\currentparskip}{\parskip} \textbf{Problem \arabic{question}.} % 左右各缩进0.05textwidth,等价于原minipage的0.9textwidth宽度 \begin{adjustwidth}{0.05\textwidth}{0.05\textwidth} }{ \end{adjustwidth} \vspace{12pt} \stepcounter{question} } % 重定义solution环境 \newenvironment{solution}{ \setlength{\currentparskip}{\parskip} \begin{adjustwidth}{0.05\textwidth}{0.05\textwidth} \setlength{\parskip}{\currentparskip} \textbf{Solution:} \\[12pt] }{ \end{adjustwidth} \vspace{24pt} }
完整可编译示例
\documentclass[12pt]{article} \usepackage{parskip} \usepackage{lipsum} \usepackage{changepage} % 新增包 \newcounter{question} \setcounter{question}{1} \newlength{\currentparskip} % 重定义question环境 \newenvironment{question}{ \setlength{\currentparskip}{\parskip} \textbf{Problem \arabic{question}.} \begin{adjustwidth}{0.05\textwidth}{0.05\textwidth} }{ \end{adjustwidth} \vspace{12pt} \stepcounter{question} } % 重定义solution环境 \newenvironment{solution}{ \setlength{\currentparskip}{\parskip} \begin{adjustwidth}{0.05\textwidth}{0.05\textwidth} \setlength{\parskip}{\currentparskip} \textbf{Solution:} \\[12pt] }{ \end{adjustwidth} \vspace{24pt} } \setlength{\parskip}{24pt} \begin{document} \begin{question} A question with an answer too long to fit on the same page as the question is on. \lipsum[100] \end{question} \begin{solution} \lipsum[100] \lipsum[100] \lipsum[100] \end{solution} \begin{question} A question with too long of a solution even for its own page. \end{question} \begin{solution} \lipsum[100] \lipsum[100] \lipsum[100] \lipsum[100] \lipsum[100] \lipsum[100] \end{solution} \end{document}
关键改动说明
- 用
adjustwidth替代minipage+center:adjustwidth是段落级环境,支持自动跨页断行,同时通过设置左右缩进实现和原minipage完全一致的宽度效果。 - 保留原有格式:问题编号、加粗标题、段落间距的设置都完全保留,只是替换了不可断页的盒子结构。
- 无需额外调整分页逻辑:LaTeX会自动处理长内容的分页,不会再出现页面留白或内容被截断的情况。
内容的提问来源于stack exchange,提问作者Edmond Dantes
相关产品推荐
相关产品推荐

