129

I have the following LaTeX code:

\documentclass{article}
\begin{document}
\begin{description}

\item[foo]
bar

\item[baz]

bang

\end{description}

\end{document}

and it produces the following PDF:

enter image description here

I want the definition of the item (in this case the "bar" and the "bang") to be on a separate line from the item (eg the "foo" and the "baz").

However this is part of a system that autogenerates documention, so I can't change the source code. I can put style things in the header of each documention, so I need some sort of LaTeX command that will make all definition list (in LaTeX speak, a "descripition list") go onto a new line.

135

You can do that with a \hfill. No extra packages or multiple lines of code are needed :)

enter image description here

\documentclass{article}
\begin{document}
\begin{description}
  \item[First] \hfill \\ The first item
  \item[Second] \hfill \\ The second item
  \item[Third] \hfill \\ The third etc \ldots
\end{description}
\end{document}

enter image description here

| improve this answer | |
  • 2
    Note that the extra \\ after the \hfill might not be necessary, since \hfill already creates a vertical space. Adding \\ might make the space disproportionately large. – xji Apr 1 '17 at 18:32
  • Indeed. It's added in a recent edit. Maybe revert needed. – Julian Apr 1 '17 at 22:54
  • changed the double \\ to a single \ , as the single \ works for almost 8 years without any problems. – Julian Apr 2 '17 at 20:43
  • 1
    @Julian The doubles backslashes \\ are necessary - try it: with only \ you get the item text in the same line. The edit history is deceiving due to the \\-bug (see tex.meta.stackexchange.com/q/7168/35864) – moewe Jul 9 '17 at 17:03
  • 1
    @moewe Now after some testing I realize the reason why the \\ would introduce an extra vertical space in my case is that I immediately started an itemize environment just after the definition. If it's plain text, then \\ would indeed be needed. Sorry for the misunderstanding. I'll just leave this comment here so that people who want to start another environment immediately after the definition could take note. Whoever would review the current edit should just reject it (An edit can't be cancelled after submitted). Thanks. – xji Jul 9 '17 at 21:21
81

Will Robertson's answer can affect all the description environments in the entire document. To apply the desired setting to a particular description, you may use [style=nextline] appended to \begin{description}

\usepackage{enumitem}

\begin{description}[style=nextline]
   \item[foo] bar
   \item[baz] bang
\end{description}

This will apply your style only to that particular description. Other descriptions will work with default behavior.

| improve this answer | |
  • 4
    It is worth noting that this does not work if your key fits within the margins. Excerpt from the enumitem Manual: nextline: if the label does not fit in the margin, the text continues in the next line. I have found this to be less than four characters. \item [Red] This field is mandatory. I assume this is a space-saver mechanism, but I do not think it is aesthetic. I would be interested in suppressing this behavior. – Jonathan Komar Aug 20 '15 at 15:38
29

If you are using enumitem, the correct way to set the default style is like this:

\usepackage{enumitem}
\setlist[description]{style=nextline}
| improve this answer | |
  • 1
    I have to say, the enumitem documentation does a poor job of explaining this behaviour: "nextline: if the label does not fit in the margin, the text continues in the next line" I read this and disregarded it because I wanted a line break regardless of the label width. Well, good thing I found your answer, thanks! – Christian Apr 16 '14 at 21:18
17

With this the label of the first level (of description environment) is aligned with the rest of the text and description texts are aligned with labels. I hope this is about what you want.

\let\origdescription\description
\renewenvironment{description}{
  \setlength{\leftmargini}{0em}
  \origdescription
  \setlength{\itemindent}{0em}
  \setlength{\labelsep}{\textwidth}
}
{\endlist}
| improve this answer | |
15

You could try something like

\usepackage{enumitem}
\setdescription{labelsep=\textwidth}

but a little more massaging is needed with the values of labelindent, etc., to avoid overfull boxes. To first order, however, this gives you the output you're looking for.

| improve this answer | |
  • 6
    this causes overfull hbox errors, you should use style=nextline – jterrace Sep 8 '12 at 20:44
2

Summarizing what others have suggested using the enumitem package, and solving the remaining inconsistency regarding short labels:

\documentclass{article}
\usepackage{enumitem}
%% Uncomment for global behaviour:
%\setlist[description]{style=nextline}

\begin{document}
    \begin{description}[style=nextline] %% Style for this list only.
        \item[Turquoise] Medium to long labels force the description to the next line.
        \item[Red] Short labels still have the description on the same line.
        \item[Red~~~] To force the description to the next line, pad the label with \textasciitilde.
    \end{description}
\end{document}

You do not name the system that autogenerates documention, but if it is Doxygen, the

\usepackage{enumitem}
\setlist[description]{style=nextline}

can go in a supplied preamble and the padding can be added manually surrounded by \latexonly and \endlatexonly:

<dl>
<dt>Zoom</dt>
<dd>Zoom by rolling the scroll wheel (third, or middle mouse button).</dd>
<dt>Pan\latexonly~~~\endlatexonly</dt>  <!-- Force description to next line. -->
<dd>Pan by pressing and holding the scroll wheel.</dd>
</dl>
| improve this answer | |
  • 1
    I don't see the need for a summary answer here, as all of these are contained in other answers. That's just how this Q&A works. – Werner Jun 12 '17 at 21:02
  • It is not all summary, the [Red~~~] part has not been brought up before, addressing the point made by @Jonathan Komar. Later I found that his problem has been answered in another way. – Bastiaan Veelo Jun 14 '17 at 7:09
1

I had issues with style=nextline.

The following technique sets an expected labelwidth of 0pt to ensure that nothing fits in the label, thereby forcing a new line.

\usepackage{enumitem}
\setlist[description]{
    style=nextline,
    labelwidth=0pt,
    leftmargin=15pt,
    itemindent=\dimexpr-5pt-\labelsep\relax,
}

See the answer here: How can I force all text to continue to the next line when using style=nextline in description lists with enumitem?

| improve this answer | |

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.