代码

代码

如果我在描述列表中嵌入了逐项列表,如何使描述的项目标签与同一页面上嵌入的逐项列表中的至少 1 或 2 个项目保持一致?

我将参考描述项目键括号内存储的信息如下:

\item [key] Some descriptive value for the key.

代码

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{xparse}% \NewDocumentCommand
\usepackage{enumitem}% \setlist
\usepackage{lipsum}% \lipsum
\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=15pt,font=\bfseries,itemindent=\dimexpr-5pt-\labelsep\relax}% Global Level 1, \dimexpr-5pt-\labelsep\relax tries to ensure nextline regardless of item key length
\NewDocumentCommand{\emptytext}{}{\mbox{}\vspace*{-\baselineskip}\leavevmode}
\begin{document}
\lipsum[1-4] 

% A TeX Loop for Fun
\newcount\N
\N=0
\loop
This line takes up space\par
\advance \N by 1
\ifnum \N<6
\repeat

\begin{description}
  \item [Numbers]
    1,2,3,4,5
  \item [Letters] \emptytext
    \begin{itemize}
      \item Do not let me stand alone on a page without a context!
      \item b
      \item c
    \end{itemize}
\end{description}
\end{document}

输出

这是输出相关部分的图片。

在此处输入图片描述

答案1

我可以提供一个导致寡妇的解决方案。正如 Ulrike 在写作时所建议的那样\begin{itemize}[beginpenalty=10000],将开始惩罚全局地放入列表中会产生不良的副作用。请注意第二个列表中的寡妇。

解决方案可能是本地化添加 beginpenalty,但我仍然不知道如何最好地实现这一点。

我尝试过

\makeatletter
\ifx\@currenvir\description % attempt at checking for environment
  \setlist[itemize,1]{beginpenalty=10000}
\fi
\makeatother

代码

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{xparse}% \NewDocumentCommand
\usepackage{enumitem}% \setlist
\usepackage{lipsum}% \lipsum
\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=15pt,font=\bfseries,itemindent=\dimexpr-5pt-\labelsep\relax}% Global Level 1, \dimexpr-5pt-\labelsep\relax tries to ensure nextline regardless of item key length

%\makeatletter
%\ifx\@currenvir\description % attempt at checking for environment
  \setlist[itemize,1]{beginpenalty=10000}
%\fi
%\makeatother

\NewDocumentCommand{\emptytext}{}{\mbox{}\vspace*{-\baselineskip}\leavevmode}
\begin{document}
\lipsum[1-4]

% A TeX Loop for Fun
\newcount\N
\N=0
\loop
This line takes up space\par
\advance \N by 1
\ifnum \N<6
\repeat
%
\makeatletter
\begin{description}
  \item [Numbers]
    1,2,3,4,5, \@currenvir
  \item [Letters] \emptytext
    \begin{itemize}
      \item Do not let me stand alone on a page without a context!
      \item b
      \item c
    \end{itemize}
\end{description}
\makeatother
%
\lipsum[1-4]

newline here

newline here

newline here

newline here, notice item 1 stays with this line due to beginpenalty=10000
\begin{itemize}
\item hello
\item goodbye, thanks, now I am a widow
\end{itemize}
\end{document}

相关内容