如何避免考试类中问题和答案选项之间出现分页符?

如何避免考试类中问题和答案选项之间出现分页符?

我想在 Exam 类中避免问题和答案选项之间的分页符。对于如何避免答案选项内的分页符问题,有一个很好的解决方案这里,但该问题并未解决如何防止问题和答案之间出现中断。

答案1

添加以下行

\usepackage{etoolbox}
\BeforeBeginEnvironment{checkboxes}{\par\nopagebreak\minipage{\linewidth}}
\AfterEndEnvironment{checkboxes}{\endminipage}

到序言中提出问题,并将复选框放在一起。

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{etoolbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment{checkboxes}{\par\nopagebreak\minipage{\linewidth}}
\AfterEndEnvironment{checkboxes}{\endminipage}

\CorrectChoiceEmphasis{}
\textheight3.4in   %% just for demo
\begin{document}
\begin{questions}
\question Random text for question 1
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 2
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\CorrectChoice answer 4
\choice answer 5
\choice answer 6
\end{checkboxes}

\question Random text for question 3
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 4
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 5
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\end{questions}

\end{document}

答案2

由于\question不是环境,因此没有很好的方法在每个问题\minipage之前和之后插入。但before可以做得很好。正如\endminipage\filbreak\questionFrank Mittelbach 对另一个答案的注释\filbreak插入低水平的拉伸(被克服\fill)和强烈的分页建议。如果 LaTeX 坚持在问题词干和其选项之间进行分页,minipage仍然可以在其周围进行硬编码。(注 1)

下一个挑战是该\question命令未在类的顶层提供exam。它仅在questions环境内。幸运的是,\questionshook提供了一个在此环境内扩展的命令。因此,将其放在序言中似乎可行:

\usepackage{etoolbox}
\appto{\questionshook}{\preto{\question}{\filbreak}}

这是一个完整的工作示例,可能不是最小的,但足够长,可以延伸到几页。(注 2)

\documentclass{exam}
\title{Know your US States and Capitals!}

\usepackage{etoolbox}
\appto{\questionshook}{\preto{\question}{\filbreak}}

\begin{document}

\begin{questions}

\question What is the capital of California?
\begin{choices}
    \choice Los Angeles
    \choice Sacramento
    \choice San Francisco
    \choice San Diego
\end{choices}

\question Which city serves as the capital of Texas?
\begin{choices}
    \choice Dallas
    \choice Houston
    \choice Austin
    \choice San Antonio
\end{choices}

\question What is the capital of Florida?
\begin{choices}
    \choice Miami
    \choice Orlando
    \choice Jacksonville
    \choice Tallahassee
\end{choices}

\question Which city is the capital of New York?
\begin{choices}
    \choice Buffalo
    \choice Albany
    \choice New York City
    \choice Syracuse
\end{choices}

\question What is the capital of Nevada?
\begin{choices}
    \choice Reno
    \choice Henderson
    \choice Carson City
    \choice Las Vegas
\end{choices}

\question Which city serves as the capital of Colorado?
\begin{choices}
    \choice Denver
    \choice Boulder
    \choice Colorado Springs
    \choice Aurora
\end{choices}

\question What is the capital of Massachusetts?
\begin{choices}
    \choice Springfield
    \choice Boston
    \choice Worcester
    \choice Cambridge
\end{choices}

\question Which city is the capital of Washington state?
\begin{choices}
    \choice Seattle
    \choice Tacoma
    \choice Spokane
    \choice Olympia
\end{choices}

\question What is the capital of Hawaii?
\begin{choices}
    \choice Maui
    \choice Honolulu
    \choice Kauai
    \choice Hilo
\end{choices}

\question Which city serves as the capital of Ohio?
\begin{choices}
    \choice Cincinnati
    \choice Columbus
    \choice Cleveland
    \choice Dayton
\end{choices}

% Additional Questions (11-20)

\question What is the capital of Arizona?
\begin{choices}
    \choice Tucson
    \choice Mesa
    \choice Phoenix
    \choice Flagstaff
\end{choices}

\question Which city serves as the capital of Pennsylvania?
\begin{choices}
    \choice Philadelphia
    \choice Pittsburgh
    \choice Allentown
    \choice Harrisburg
\end{choices}

\question What is the capital of Illinois?
\begin{choices}
    \choice Chicago
    \choice Springfield
    \choice Rockford
    \choice Peoria
\end{choices}

\question Which city is the capital of Oregon?
\begin{choices}
    \choice Eugene
    \choice Salem
    \choice Portland
    \choice Bend
\end{choices}

\question What is the capital of Tennessee?
\begin{choices}
    \choice Memphis
    \choice Knoxville
    \choice Chattanooga
    \choice Nashville
\end{choices}

\question Which city serves as the capital of New Jersey?
\begin{choices}
    \choice Newark
    \choice Trenton
    \choice Jersey City
    \choice Atlantic City
\end{choices}

\question What is the capital of Wisconsin?
\begin{choices}
    \choice Milwaukee
    \choice Green Bay
    \choice Madison
    \choice Kenosha
\end{choices}

\question Which city is the capital of Minnesota?
\begin{choices}
    \choice Rochester
    \choice St. Paul
    \choice Duluth
    \choice Minneapolis
\end{choices}

\question What is the capital of Utah?
\begin{choices}
    \choice Salt Lake City
    \choice Provo
    \choice Ogden
    \choice St. George
\end{choices}

\question Which city serves as the capital of Maryland?
\begin{choices}
    \choice Baltimore
    \choice Annapolis
    \choice Rockville
    \choice Frederick
\end{choices}

\end{questions}    
\end{document}

笔记:

  1. LaTeX 允许你将命令假装成环境。\begin{question}启动一个组并扩展\question,并且(由于\endquestion未定义)\end{question}结束该组并且不执行任何其他操作。如果你喜欢使用环境,那么这很不错。

  2. ChatGPT 知道如何使用该exam课程!

相关内容