0

I've seen the question (and this one) explaining how to break the line after the list label but sometimes I have labels for my items that fill the whole line in which case I don't want to create an empty line. Is there a way to insert a command that only breaks the line if a full line isn't taken up so the author doesn't have to manually adjust the code depending on margins etc..

For instance, in this case the blank line after the second Case statement is undesired. example with undesired line break

This was produced with the following code.

\documentclass{amsart}
\usepackage{xparse}
\usepackage{amsmath,amssymb}
\usepackage{enumitem}
\usepackage{unicode-math}
    \setmainfont{XITS}
    \setmathfont{XITS Math}
    \setmathfont{XITS Math}[range={\mathscr,\mathbfscr}]
    \setmathfont{XITS Math}[range={\mathcal,\mathbfcal},StylisticSet=1]

\makeatletter

\newlist{pfcasesnonum}{enumerate}{3}
        \setlist[pfcasesnonum]{
          label={Case}\protect\thiscase:~,
          align=left,
          labelsep=!,
          itemindent=-1em,
          leftmargin=2em,
          labelindent=0pt,
          labelwidth=0pt
        }
        \setlist[pfcasesnonum,1]{
            before=\def\pfcasecounter@pmg{pfcasesnonumi},
        }
\newenvironment{pfcases}{
            \ProvideDocumentCommand{\case}{o}{
                    \def\thiscase{~##1}%
                \item  \protected@edef\@currentlabel{##1}
        }
        \begin{pfcasesnonum}
        }
        {\end{pfcasesnonum}}
\makeatother

\begin{document}
 % \directlua{tex.write("2020-02-24 v1.15")}
% \@ifundefinedcolor{Black}{undefined}{defined}

\begin{pfcases}
\case[foo foo foo foo foo foo foo foo foo foo foo foo] \mbox{} \\bar bar
\case[foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo] \mbox{} \\bar bar
\end{pfcases}

\end{document}

I want the user to be able to use the same code to get the case statement on it's own line without needing to check if it fills the whole line or not.

Note that this code has been considerably simplified (but I may have left in some unnecessary aspects) to remove extraneous features such as numbering, having the reference repeat the case condition which is why I'm bothering to define this environment in the first place. Ideally, there would be some way I could modify the case command so that users who don't have deep tex knowledge can do this with a simple \\ or something like \hfil \\ or \leavevmode \\.

2 Answers 2

1

The issue is a \penalty which is inserted after the label to ensure that a break is done there if the label fills an entire line. This is normally desirable, but since you also add a \penalty (through \\) you end up allowing two linebreaks. This can be avoided by removing the existing \penalty using \unpenalty. This requires you to use \leavevmode instead of \mbox{} to leave vmode, since otherwise the box "hides" the \penalty. Here I defined a new command to make this easier:

\documentclass{amsart}
\usepackage{xparse}
\usepackage{amsmath,amssymb}
\usepackage{enumitem}
\usepackage{unicode-math}
    \setmainfont{XITS}
    \setmathfont{XITS Math}
    \setmathfont{XITS Math}[range={\mathscr,\mathbfscr}]
    \setmathfont{XITS Math}[range={\mathcal,\mathbfcal},StylisticSet=1]

\makeatletter

\newlist{pfcasesnonum}{enumerate}{3}
        \setlist[pfcasesnonum]{
          label={Case}\protect\thiscase:~,
          align=left,
          labelsep=!,
          itemindent=-1em,
          leftmargin=2em,
          labelindent=0pt,
          labelwidth=0pt
        }
        \setlist[pfcasesnonum,1]{
            before=\def\pfcasecounter@pmg{pfcasesnonumi},
        }
\newenvironment{pfcases}{
            \ProvideDocumentCommand{\case}{o}{
                    \def\thiscase{~##1}%
                \item  \protected@edef\@currentlabel{##1}
        }
        \begin{pfcasesnonum}
        }
        {\end{pfcasesnonum}}

\makeatother

\NewDocumentCommand \adjustednewline {} {%
    \leavevmode  % Start the paragraph -- avoids "no line here to end"
    \unskip      % Not necessary when only used after \item, but it's a bit safer when used in other situations
    \unpenalty   % If we already had a penalty, remove it
    \newline     % Finally break the line
}

\begin{document}
 % \directlua{tex.write("2020-02-24 v1.15")}
% \@ifundefinedcolor{Black}{undefined}{defined}

\begin{pfcases}
    \case[foo foo foo foo foo foo foo foo foo foo foo foo] \adjustednewline bar bar
\case[foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo] \adjustednewline bar
\end{pfcases}

\end{document}
0
1

Not at all a list environment, but maybe enough:


mwe


\documentclass{amsart}
\usepackage{parskip,lipsum}
\def\case#1{\par\noindent\hangindent2em{Case %
{\em  #1}:}\hfill\mbox{}\linebreak} 
\begin{document}
\case{\lipsum[3][1]} \lipsum[2][1-3]
\case{\lipsum[8][2] Ad hoc finis} \lipsum[2][1-3]
\case{\lipsum[3][1-2]} \lipsum[2][1-3]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .