2

I have defined a very simple custom environment for homework problems, which is basically just enumerate but without the indents:

\newcounter{hwprob}
\newenvironment{hwprob}{\refstepcounter{hwprob} \textbf{\thehwprob.} ~ }{}

If I open an actual enumerate environment inside hwprob, this happens:

\begin{hwprob}
   \begin{enumerate}
      \item Some text.
      \item Some other text.
   \end{enumerate}
\end{hwprob}

I'm not sure why the first item 1. does not start on the same line as 1. I tried removing the tilde, but to no avail. What puzzles me is that I know that when using enumerate inside the proof environment the first item will be in the same line as Proof., with the correct indentation and all.

What can I do to fix this? Is this a result of how I defined hwprob, or is it inherent to enumerate?

1
  • 2
    Welcome to TeX.SE.
    – Mico
    Commented Sep 15, 2020 at 15:55

2 Answers 2

4

Here is the "how" part: define hwprob as a list environment, and auto insert an \item at the beginning of hwprob.

\documentclass{article}
\usepackage{enumitem}

\newlist{hwprob}{enumerate}{1}
\setlist[hwprob]{label=\textbf{\arabic*.}, first*=\item}

\begin{document}
\begin{hwprob}
   \begin{enumerate}
      \item Some text.
      \item Some other text.
   \end{enumerate}
\end{hwprob}
\end{document}

enter image description here

For the "why" part, I guess it has something to do with \trivlist (perhaps with \parshape), which is the common base used by both list and theorem environments.

2
  • 2
    The LaTeX design treating many paragraph-type elements as lists causes innumerable problems, and (I believe) should be considered a misfeature. Commented Sep 15, 2020 at 16:35
  • Thanks! This works. Commented Sep 15, 2020 at 16:43
6

The formatting you wish to achieve -- have the first item in the enumerate list start on the current line rather than start after a line break -- happens if the enumerate environment is started inside one LaTeX's list or trivlist environments.

To achieve your formatting objective, I suggest you load the enumitem package and use its \newlist and \setlist macros to create a bespoke enumerate-like list environment called hwprob.

enter image description here

\documentclass{article}
\usepackage{enumitem}
\newlist{hwprob}{enumerate}{1}
\setlist[hwprob,1]{label=\bfseries\arabic*.,left=0pt}

\begin{document}
\begin{hwprob}
\item
   \begin{enumerate}
      \item Some text.
      \item Some other text.
   \end{enumerate}
\end{hwprob}
\end{document}
1
  • 1
    Thank you! Unfortunately I can't mark both answers as correct, for some reason, but you sure have my gratitude as well. Commented Sep 15, 2020 at 16:44

You must log in to answer this question.

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