2

Consider the following MWE

\documentclass[preview,border=10pt]{standalone}
\usepackage{enumitem}
\begin{document}
\thispagestyle{empty}

\trivlist\item[Boring dummy text]
\begin{enumerate}
 \item Foo
 \item Bar
 \item Quux
\end{enumerate}
\endtrivlist

\end{document}

picture of the result

To me, this behaviour is a feature! I am defining my own command \paragraph{#1}, and if the paragraph start with a list, I would like to have the first item of the list to start on the same line as the paragraph header.

The easy option: instead of using a command, define an environment paragraph that takes an option [#1] for the paragraph header. Now typeset the header of the paragraph using \trivlist\item[#1], and close the environment using \endtrivlist.

However, I don't like this option. It breaks the default markup where \paragraph is a command. And of course I don't want users to manually type \endtrivlist to close a paragraph. This brings me to my question:

Q. How can I reproduce the behaviour of \trivlist\item[Dummy text] without actually opening and closing a list?

So, I am looking for a command \paragraph{#1} such that the following MNWE will produce the same output as above.

\documentclass[preview,border=10pt]{standalone}
\usepackage{enumitem}
\begin{document}
\thispagestyle{empty}

\paragraph{Boring dummy text}
\begin{enumerate}
 \item Foo
 \item Bar
 \item Quux
\end{enumerate}

\end{document}
  • Off-topic: Most LaTeX classes already provide a command called \paragraph. Repurposing it in the way you suggest might cause lots of headache in lots of existing documents. – Mico Apr 12 '18 at 9:30
  • @Mico I completely agree. So “lots of existing documents” should not use my definition of \paragraph (whose existence currently depends on whether this question has a satisfying answer). – jmc Apr 12 '18 at 9:37
  • The above is also standard behaviour without the enumitem package. – Andrew Swann Apr 12 '18 at 10:05
  • If I understand well your problem, using \trivlist\item[Boring dummy text] \leavevmode should do the trick. – Bernard Apr 12 '18 at 10:08
5

You could emulate, in your command, what \item does internally.

Edit: The previous version of this code had been written hastily and contained some inconsistencies. I’ve now made the \myparagraph command more similar, in behavior, to \paragraph and, at the same time, I’ve streamlined the code.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
\usepackage[utf8]{inputenc}      % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{lipsum} % just for dummy "Lorem ipsum..." text

\makeatletter

\newcommand*\myparagraph[1]{%
    \par
    \addpenalty\@secpenalty
    \addvspace\bigskipamount
    \global\setbox\@labels\hbox{%
        \color@begingroup
        \textbf{#1}%
        \color@endgroup
    }%
    \global\@inlabeltrue
    \everypar{%
        \if@inlabel
            \global\@inlabelfalse
            {\setbox\z@ \lastbox}% remove indentation (OK?)
            \unhbox\@labels
            \quad
        \fi
        \everypar{}%
    }%
    \ignorespaces
}

\makeatother



\begin{document}

Some text.

\myparagraph{With a list following immediately.}
\begin{enumerate}
    \item  First item.
    \item  Second item.
    \item  Third item.
\end{enumerate}

\myparagraph{With a list \emph{not} following immediately.}
Some text
% \showthe\everypar
in between.
\begin{enumerate}
    \item  First item of second list.
    \item  Second item of second list.
    \item  Third item of second list.
\end{enumerate}

\myparagraph{With longer text.}
\lipsum*[1]
\begin{enumerate}
    \item  First item of third list.
    \item  Second item of third list.
    \item  Third item of third list.
\end{enumerate}
\lipsum*[2]

\end{document}

The output:

Output of the code

In a comment, @jmc asks to explain how this code works. Well, as I said, it emulates what the \item command does internally to ensure that a list environment immediately following it does not start a new line. To give further detail, I’d have to explain, at some length, how lists are dealt with in LaTeX, and I’m afraid that this is—so to speak—“beyond the scope of this answer”. (:-) LaTeX’s management of list environments, originally designed some 35/40 years ago by Leslie Lamport, is explained, albeit very tersely, in the comments in the sources of the LaTeX kernel, more specifically those found in the file ltlists.dtx. Recall that, with TeXLive installed, you can view a typeset version of the complete sources for the LaTeX kernel by typing

texdoc source2e

at a command-line prompt.

Perhaps I’ll be able to review this answer in the next days and add further explanations.

  • I have no idea why this works… but it works like a charm! Thanks a lot. Could you please annotate the code, to explain how the ‘trick’ works? – jmc Apr 12 '18 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.