30

Quite a lot environments which change margins (e.g. center, quote, addmargin from KOMA) use internally trivlist. This has some drawbacks, e.g., when the list is directly between two sectioning commands:

\documentclass{article}
\begin{document}
\section{a section}
%a
\begin{quote}
\subsection*{An important quote}
blalbla
\end{quote}
\end{document}

e.g., gives the well known and feared error:

! LaTeX Error: Something's wrong--perhaps a missing \item.

So I tried to extract from trivlist the code that does the indentation and drop all the label related code and came up with the following code. My question is:

Is there some obvious flaw?

(It looks so simple that I wonder why it doesn't exist yet!)

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\newenvironment{indentation}[2][0pt]%#1=right indentation
  {\par
   \begingroup
   \global\advance\@listdepth\@ne  
   \leftmargin=#2\relax
   \rightmargin=#1\relax
   \advance\linewidth -\rightmargin\relax
   \advance\linewidth -\leftmargin
   \advance \@totalleftmargin \leftmargin
   \parshape\@ne \@totalleftmargin \linewidth
   \@setpar{{\@@par}}}
  {\global\advance\@listdepth\m@ne\endgroup\par}
\makeatother

\begin{document}
\section{abc}
  \lipsum[1]
   \[a=b\]
\begin{itemize}
\item blabla
\end{itemize}


\section{abc}
\begin{indentation}[\leftmargini]{\leftmargini}
\subsection{abc}
 \lipsum[1]
 \begin{itemize}
  \item blub
   \begin{enumerate}
    \item Does it work?
   \end{enumerate} 
 \end{itemize}
 \[a=b\]
 \lipsum[2-3]

 \begin{indentation}{0pt}
  \centering
  \lipsum[1]
 \end{indentation}
\end{indentation}

\end{document}

Edit

I take back the "doesn't exist yet". I just found on CTAN http://www.ctan.org/tex-archive/macros/latex209/contrib/misc/indent.sty, which contains an almost identical definition (even with the same name). The only difference is that my code changes \@listdepth as I wanted to keep the relation of indent for nested lists.

rebatoma
  • 351
Ulrike Fischer
  • 327,261
  • 6
    bravo for daring to escape from the list straitjacket. it's an oversimplification to think that everything is just another form of list. (so many reported bugs for amsthm are simply the result of list being too restrictive.) i haven't actually tried compiling this, but from reading the code, i didn't see anything obvious either. i will try to find time to give it a workout. – barbara beeton Apr 08 '15 at 17:19
  • 2
    why do you want a subsection heading inside a quote? – David Carlisle Apr 08 '15 at 17:25
  • 1
    The obvious flaw is having a \subsection inside a quote. It makes no sense. – egreg Apr 08 '15 at 17:36
  • @DavidCarlisle: In real life I would probably use a KOMA class and \minisec. Beside this the example should only show that it is dangerous to start one of the environments based on trivlist with a sectioning command. Also I get quite often the request to indent some part of a document and then fight against the additional list spacing and the \@noitemerr. – Ulrike Fischer Apr 08 '15 at 17:38
  • @egreg: And what would you use as title of quote if you want to prevent that there is a page break after it? – Ulrike Fischer Apr 08 '15 at 17:39
  • 1
    Surely not \subsection*. I would define a titledquote environment, with a proper setting for avoiding page breaks (\nopagebreak might be sufficient). Abusing sectioning commands is wrong to begin with. – egreg Apr 08 '15 at 17:41
  • 4
    I should say starting sections inside a lower level environment should be a non-aim, so while your code looks probably safe enough I'm not sure I agree with the premise that it's needed:-) – David Carlisle Apr 08 '15 at 17:45
  • @DavidCarlisle: I'm starting to regret that I used this example to demonstrate the problems with \trivlist;-). But perhaps this one is more convincing: In this question the internal center environment is the culprit: http://tex.stackexchange.com/questions/237528/tikz-label-over-path-flips. If I redefine center to \renewenvironment{center}{\begin{indentation}{0pt}\centering}{\end{indentation}} the problem disappears. – Ulrike Fischer Apr 08 '15 at 18:02
  • 1
    Section headings in quotes will be rare, but it is in the nature of a quote that we don't have control over its contents. An extended quote may well contain some sort internal of section headings. – Michael Palmer Nov 02 '16 at 00:39

1 Answers1

1

If you remove the % from line number 4 of your code, the error goes away. A \section always has to contain some text.

  • 1
    Sorry but this is not an answer. I know that the error goes away if I uncomment the "a". But the point of my question it to avoid the error in the other case. – Ulrike Fischer May 23 '17 at 09:33
  • If you want to have an empty \section, put something like ~\ in it: \begin{document} \section{a section} ~\ \begin{quote} ...

    It won't appear int the pdf.

    – Sweidán Omár May 23 '17 at 09:35
  • My question is not about document based work arounds but about a general solution. Please check my reputation before continuing to give me such advices. – Ulrike Fischer May 23 '17 at 09:45
  • Sorry for the misunderstanding. Then you should define your own section command: \documentclass{article}

    \newcommand\mysection[2][\DefaultOpt]{% \def\DefaultOpt{#2}% \section[#1]{#2}% }

    \begin{document} \mysection[Empty section]{ %a \begin{quote} \subsection*{An important quote} blalbla \end{quote} } \end{document}

    – Sweidán Omár May 23 '17 at 11:09
  • You are again offering a solution for one document. And I certainly don't want to put the whole section text in an argument. – Ulrike Fischer May 23 '17 at 12:20