1

I have a fundamental problem in understand how latex is typesetting documents. Consider my code

\documentclass{article}
\usepackage[margin = 0.75in]{geometry}
\begin{document}

\section*{ABC}
abc
\section*{ABC}
\begin{itemize}
    \item abc
\end{itemize}
\textbf{ABC}\\
abc\\  
\textbf{ABC}\\
\begin{itemize}
    \item abc
\end{itemize}
\end{document}      

The output it produces is :-
latex-out

Now this is fundamentally problematic. When I changed

\section*{ABC} 
abc

to

\section*{ABC}
\begin{itemize}
    \item abc
\end{itemize}

then only the abc got a bullet beside it without any changes in formatting.

However, when i do the same operation after a new line , as in the code

\textbf{ABC}\\
abc\\  
\textbf{ABC}\\
\begin{itemize}
    \item abc
\end{itemize}

then for some reason the itemize is adding a new line before rendering the list. Why?

  • 4
    Why would you put a forced line break before an itemize environment anyway? More to the point, why use forced line breaks at all? – Ian Thompson Aug 14 '14 at 9:46
  • @IanThompson I have a general format (defined using newcommand) in which I am printing the header using \textbf{XYZ}\\ and then writing some content after that. In some cases the content starts with itemize. So even though I wouldn't use a linebreak before an itemize, it's the format which is making me do so. In any case, I would like to understand what is happening. – Navin Chandak Aug 14 '14 at 9:50
  • 1
    \textbf{XYZ}\\ isn't a very good way to produce a header. – Ian Thompson Aug 14 '14 at 9:52
  • It's happening because you're enforcing a line break ?? – 1010011010 Aug 14 '14 at 10:30
  • @IanThompson Two things. 1) What is the better way? subsections? Basically I had a \textbf{ABC}\hfill{2014-15}\\ . If I don't use the line break after the above line and use itemize after that, then I get a space character after the 2014-15 in the same line which I don't want, forcing me to use a line break after 2014-15. How to get rid of this problem? 2)I would still want to understand why this is happening – Navin Chandak Aug 14 '14 at 11:10
4

Let's see what happens when you type \begin{itemize. Now that should turn into \itemize, whereas \end{itemize} turns to \enditemize. Let's see what \itemize does:

\def\itemize{%
  \ifnum \@itemdepth >\thr@@\@toodeep\else
    \advance\@itemdepth\@ne
    \edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
    \expandafter
    \list
      \csname\@itemitem\endcsname
      {\def\makelabel##1{\hss\llap{##1}}}%
  \fi}

This is taken from latex.ltx, a file of LaTeX kernel definitions. So it checks if you are beyond the third level of itemize and if so issues the \@toodeep error, otherwise it advances the counter for the itemize level and defines \@itemitem to be labelitem plus \@itemdepth in roman numerals. Then it uses the \list command with arguments \csname\@itemitem\endcsname, i.e. \<whatever there is in @itemitem>, e.g. \labelitemi if \@itemdepth is 1, which is the reason for the \expandafter which is needed to expand \@itemitem before \list takes its arguments, and the second argument is {\def\makelabel##1{\hss\llap{##1}}, where \hss is a variable space which sort of decides where to put the item label, and \llap means the label can go outside the left margin. So let's look at \list, again from latex.ltx.

\def\list#1#2{%
  \ifnum \@listdepth >5\relax
    \@toodeep
  \else
    \global\advance\@listdepth\@ne
  \fi
  \rightmargin\z@
  \listparindent\z@
  \itemindent\z@
  \csname @list\romannumeral\the\@listdepth\endcsname
  \def\@itemlabel{#1}%
  \let\makelabel\@mklab
  \@nmbrlistfalse
  #2\relax
  \@trivlist
  \parskip\parsep
  \parindent\listparindent
  \advance\linewidth -\rightmargin
  \advance\linewidth -\leftmargin
  \advance\@totalleftmargin \leftmargin
  \parshape \@ne \@totalleftmargin \linewidth
  \ignorespaces}

Again, there is a check as to whether you are nesting more than 5 lists, if so we have \@toodeep, otherwise we advance the number of nested lists by \@ne, i.e. 1. Then we have three lengths set to 0 (\z@ is 0pt), then the command \@list<roman numeral of list depth> (e.g. \@listi if we are on the first level of nesting) is used. \@listi and so forth are not in latex.ltx, nor in plain.tex or list.tex, so I don't know where to look. \@itemlabel is defined as the first argument, then \makelabel is set to \@mklab, which is \def\@mklab#1{\hfil #1}. The conditional \@nmbrlist is made false, argument 2 is executed, with a \relax probably to separate it from what follows, then \@trivlist is executed, then there are a couple of length assignments, and then there is \ignorespaces. Now \@trivlist, again in latex.ltx, is defined as follows:

\def\@trivlist{%
  \if@noskipsec \leavevmode \fi
  \@topsepadd \topsep
  \ifvmode
    \advance\@topsepadd \partopsep
  \else
    \unskip \par
  \fi
  \if@inlabel
    \@noparitemtrue
    \@noparlisttrue
  \else
    \if@newlist \@noitemerr \fi
    \@noparlistfalse
    \@topsep \@topsepadd
  \fi
  \advance\@topsep \parskip
  \leftskip \z@skip
  \rightskip \@rightskip
  \parfillskip \@flushglue
  \par@deathcycles \z@
  \@setpar{\if@newlist
             \advance\par@deathcycles \@ne
             \ifnum \par@deathcycles >\@m
               \@noitemerr
               {\@@par}%
             \fi
           \else
             {\@@par}%
           \fi}%
  \global \@newlisttrue
  \@outerparskip \parskip}

I won't try to explain exactly what this does. I only see that \@newlisttrue is executed. \item, besides a few uninteresting things, calls \@item, which is:

\def\@item[#1]{%
    \if@noparitem
         \@donoparitem
    \else
        \if@inlabel
            \indent \par
        \fi
        \ifhmode
            \unskip\unskip \par
         \fi
         \if@newlist
              \if@nobreak
                   \@nbitem
              \else
                   \addpenalty\@beginparpenalty
                   \addvspace\@topsep
                   \addvspace{-\parskip}%
             \fi
         \else
             \addpenalty\@itempenalty
             \addvspace\itemsep
         \fi
         \global\@inlabeltrue
    \fi
 […]
}

With \@newlisttrue, we have \@topsep added as vertical space, supposing \@nobreakfalse, which gives us the vertical space. In the omitted part of \@item there is a \@newlisttrue, which means the following items only have \itemsep as vertical space. In fact, \@donoparitem has a \vskip\@tempskipa which is what gives the extra vertical space. That is used when you have two lists in a row, because there is "no par[agraph]" in between, and maybe after a \\, but anyway it's just 2pt extra with respect to \@topsep. So if you give [-10pt] as optional argument to \\ in the header, the space should vanish. Anyway, as I said in the comment, a \subsubsection might be a better idea, or if you have a command for this header format you may want to make a star variant without the \\. As for the "it doesn't always have a linebreak" part, we can look at the definition of \section. I'll copy here the one from the report class:

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

So essentially a call to \@startsection. What is done directly by that command doesn't concern us, for all I can see. It ends by calling \@sect: \@sect{#1}{#2}{#3}{#4}{#5}{#6}. Looking at the definition of \@sect, it seems to do, in any case, nothing that influences \item or \@item or \@donoparitem. So we get to the end, and see \@xsect{#5}. Again, all this does that interests us is \@tempskipa #1\relax. Anyway it seems to set \@noparitemfalse, as a simple test demonstrates. So let me look at the definition of \@item in the hypothesis of \@noparitemfalse. Immediately after a \section command, a simple test shows \ifhmode is true and the other conditionals in the above part of the definition of \@item are all false. Which means all that part does is \unskip\unskip \par and \global\@inlabeltrue. But that is not \section's fault. So it must be the default, and other commands take care of those conditionals. Which means answering this question gets very complicated and involves investigating every use of those conditionals. So I can't end this. And that was the "why does this happen" part, or the start of it.

As for the "how do I solve this" part, try this:

\documentclass[a4paper]{report}

\makeatletter
\newcommand{\header}{\@ifstar{\@header}{\he@der}}
\newcommand{\@header}[1]{\textbf{#1}\hfill2014-15\hfil}
\newcommand{\he@der}[1]{\textbf{#1}\hfill2014-15\\}
\makeatother

\begin{document}
\noindent\header{Header 1}
Text for header 1, no itemize. \\
\header*{Header 2}
\begin{itemize}
\item Here is a Header with itemize. As you can see, no extra space is produced, and no extra vertical space is added.
\item Just remember the star after \verb"header" in this case.
\end{itemize}
Now let's put a simple paragraph: Lorem ipsum dolor sit amet, consectetuer and so on. \\
\header{Header 3}
Nothing strange happens at all. I hope this solves your problem. Naturally, you can add the \verb"\noindent" to the command definition, to avoid the \verb"\parindent" for a header. You can also add a \verb"\par" or \verb"\\" before the header.
\end{document}

With this MWE, I get:

enter image description here

If that is not the format you want, you can tell me. Anyway the extra space is not present, and the extra space with itemize is not present. Of course, the header is treated as text, hence the vertical space between itemize and header. If you don't want that, I can try meddling with those conditionals mentioned above, or you can just add a \vspace to \header so that it appears only when you want it to appear.

  • 1
    For more info, you can look at latex.ltx and see the full definition of \item, \@item and \@donoparitem. – MickG Aug 14 '14 at 12:58
2

http://imgur.com/tlFDDUL

\documentclass{article}
\usepackage[margin = 0.75in]{geometry}
\begin{document}

\section*{ABC}
abc
\section*{ABC}
This is a really long paragraph depicting how \LaTeX\ typesets documents. This is a really long paragraph depicting how \LaTeX\ typesets documents. This is a really long paragraph depicting how \LaTeX\ typesets documents. This is a really long paragraph depicting how \LaTeX\ typesets documents.
\begin{itemize}
    \item abc
\end{itemize}
This is a really long paragraph that does not get indented because \LaTeX\ just left vertical mode from the \texttt{itemize} environment. This is a really long paragraph that does not get indented because \LaTeX\ just left vertical mode from the \texttt{itemize} environment. This is a really long paragraph that does not get indented because \LaTeX\ just left vertical mode from the \texttt{itemize} environment. 

This is a really long paragraph that shows how a whitespace between two paragraphs causes the second paragraph to get indented. No further formatting is needed. This is a really long paragraph that shows how a whitespace between two paragraphs causes the second paragraph to get indented. No further formatting is needed. This is a really long paragraph that shows how a whitespace between two paragraphs causes the second paragraph to get indented. No further formatting is needed.

A double slash \texttt{\textbackslash\textbackslash} forces a line break. A forced line break with the \texttt{itemize} environment subsequent thereto will first cause a line break, then \LaTeX\ will go into vertical mode to typeset the items. This involves an operation in vertical mode \emph{twice} rather than \emph{once}, as you intended it: \\
\begin{itemize}
    \item abc
\end{itemize}
Ufff, what an ugly space! Lesson learnt, I'd say!
\end{document}
  • Hi. Appreciate your effort! :) But I don't understand why latex needs to create a line break if the itemize appears after a forced line break. The answer by @mickg is technical and I am looking for the reason why latex does something like this. – Navin Chandak Aug 14 '14 at 11:07
  • Well if you are looking for the reason why it does so after a linebreak the point is it always does. I don't know if there is a way to check if just before a command another one has been executed, and thus to avoid the vertical space if \` has been used, but then one would need to redefine \itemize` and that would be rather hard. If you are looking for a reason why it does that in general, well, I guess it makes sense to put some vertical space before a list in general. Anyway does your format allow you to add a […] after the linebreak? If so, you can rule that vertical space explicitly. – MickG Aug 14 '14 at 11:27
  • You probably know, but \` can accept an optional parameter for vertical space after the linebreak, so you can specify a negative amount to eliminate the undesired vertical space. Otherwise, \subsection*` or \subsubsection could be better ideas for headers. – MickG Aug 14 '14 at 11:28
  • @MickG I don't think that it always has a line break. Because when I use itemize after a \section*{XYZ}, then itemize doesn't start with a linebreak. (the itemized text starts from the same place as where a simple text would have started) – Navin Chandak Aug 14 '14 at 12:26
  • A slightly unrelated question. Is there a way to simulate section/subsection by a combination of font-size font-style and line breaks? (Because a section seems to have a very typical behaviour and doesn't seem to be a text followed by a line-break) – Navin Chandak Aug 14 '14 at 12:36

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.