我是 LaTeX 新手,所以我感謝您的幫助:
每當您在 LaTeX 中鍵入普通文字時,如果到達行尾,它會自動中斷到下一行。但是,如果您在行尾使用框(例如 \raisebox),它會強制框進入同一行,即使已到達行尾。 (據我所知,這會導致“Overfull \hbox”警告。)
\documentclass{article}
\begin{document}
This is some text that automatically goes in the next line if the end of the line is reached.
\raisebox{0.1ex}{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above the edge of the paper.}
\end{document}
有沒有辦法自動打破行尾的盒子並繼續下一行的盒子?如果不是,至少有一種方法可以自動將整個盒子放到下一行嗎?
背景:我是一名老師,我正在嘗試為我的學生創建「填空」任務。我透過輸入普通文字然後使用以下命令來執行此操作:
\newlength{\diebox}
\newcommand{\blank}[1]{
\settowidth{\diebox}{#1}
\ifprintanswers
\raisebox{0.1ex}{\parbox{2.3\diebox}{\textbf{#1}}}
\else
\raisebox{-0.5ex}{\parbox{2.3\diebox}{\hrulefill}}
\fi}
(\ifprintanswers 來自考試包)
我使用因子“2.3”,這樣我的學生就有更多的空間來寫答案。
這確實按預期工作,除了在行尾我的答案/ hrulefill 不會中斷到下一行。
答案1
我透過使用遞歸為答案中的每個單字創建一個新框來解決這個問題,而不是只創建一個大的水平框。我也必須\allowbreak
在盒子之間。此外,使用 2.3 乘數往往會破壞利潤,因此我會盡量\sloppy
避免這種情況。
\documentclass{exam}
\newcommand\blankit[1]{\blankitaux#1 \relax}
\def\blankitaux#1 #2\relax{%
\blank{#1}%
\ifx\relax#2\relax\def\next{}\else\def\next{\blankitaux#2\relax}\fi
\next
}
\newcommand{\blank}[1]{\allowbreak
\setbox0=\hbox{#1}%
\ifprintanswers
\makebox[2.3\wd0][l]{\textbf{#1}\dotfill}%
\else
\raisebox{-0.5ex}{\makebox[2.3\wd0]{\hrulefill}}%
\fi
}
\begin{document}
\sloppy
This is some text that automatically goes in the next line if the end of the line is reached.
\blankit{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above and beyond the edge of the paper.}
Returning to normal text.
\printanswerstrue
This is some text that automatically goes in the next line if the end of the line is reached.
\blankit{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above and beyond the edge of the paper.}
Returning to normal text.
\end{document}