考虑一下这个 MWE
\documentclass[a4paper]{article}
\usepackage[top=1in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{enumerate}
\usepackage[ngerman]{babel}
\usepackage{ifthen} % for conditionals in newcommand
\usepackage[utf8]{inputenc}
\newcommand{\point}[1]{\ifthenelse{\equal{#1}{1}}{\hfill \mbox{(\emph{#1 point})}}{\hfill \mbox{(\emph{#1 points})}}} % command to display points
\begin{document}
\begin{enumerate}[(a)]
\item This is how it is mostly the case. \point{1}
\item Here, I want the \textit{(2 points)} to be aligned on the right side. This sometimes happens\ldots \point{2}
\end{enumerate}
\end{document}
我希望“(2 points)”在右侧对齐,也就是红框所在的位置。但是,我不知道如何修改命令\point
以实现我想要的效果。简单地将 包括到\hfill
中\mbox
会产生与上面相同的输出。
那么,我该如何修改\point
命令,以便始终将文本对齐在右侧 - 即使在“(x 点)”部分之前有一个换行符?
答案1
这会向右移动与标签大小相等的量并放置一个\mbox{}\allowbreak
(可能强制换行)。然后它会向左移动该量,并\hfill
在标签之前发出一个。
\documentclass[a4paper]{article}
\usepackage[top=1in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{enumerate}
\usepackage[ngerman]{babel}
\usepackage{ifthen} % for conditionals in newcommand
\usepackage[utf8]{inputenc}
\newcommand{\point}[1]{\ifthenelse{\equal{#1}{1}}%
{\def\tmpsfx{}}{\def\tmpsfx{s}}%
\setbox0=\hbox{(\emph{#1 point\tmpsfx})}%
\hspace{\wd0}\mbox{}\allowbreak\hspace{-\wd0}\hfill\box0%
} % command to display points
\begin{document}
\begin{enumerate}[(a)]
\item This is how it is mostly the case. \point{1}
\item Here, I want the \textit{(2 points)} to be aligned on the right side. This sometimes happens\ldots \point{2}
\end{enumerate}
\end{document}
答案2
感谢@UlrikeFischer 的回答另一个问题,我能够根据\quelle
上面链接中提到的答案中提供的命令解决问题:
\newcommand\toRight[1]{{%
\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil #1%
\parfillskip=0pt \finalhyphendemerits=0 \par}}
\newcommand{\point}[1]{\ifthenelse{\equal{#1}{1}}{\toRight{\mbox{(\emph{#1 point})}}}{\toRight{\mbox{(\emph{#1 points})}}}} % command to display points