# Subsection throws LaTeX Error: There's no line here to end

I have section and subsection commands in my LaTeX code described as follows. These are included in the style file, foo.cls:

\newenvironment{aside}{%
\let\oldsection\section
\renewcommand{\section}[1]{
}
\begin{textblock}{3.6}(1.5, 4.33)
\begin{flushright}
\obeycr
}{%
\restorecr
\end{flushright}
\end{textblock}
\let\section\oldsection
}

\renewcommand{\section}[1]{
\par\vspace{\parskip}
{%
\@sectioncolor #1%
}
\par\vspace{\parskip}
}

\renewcommand{\subsection}[1]{
\par\vspace{.5\parskip}%
\par\vspace{.25\parskip}%
}


When I try and use these commands in the following setting, I ran into an error:

\documentclass{foo}

\begin{document}
\begin{aside}
\section{foo}
bar
~
\section{foo}
\subsection{bar}
\end{aside}
\end{document}


Specifically the error I am encountering is:

LaTeX Error: There's no line here to end.

l.55 \subsection{foo}


What's going wrong here?

• Welcome to TeX.SX! With the “usual” setting, nothing above should throw that error, so please make a compilable example that demonstrates the problem. (As a side note, you should definitely not redefine \section as you are doing...) – Phelype Oleinik Nov 30 '19 at 20:21
• To add to Phelype Oleinik’s comment: Maybe you should take a look at the titlesec package. – Jasper Habicht Nov 30 '19 at 20:25
• I've revised my question to give a minimal working example – Jacob Denson Nov 30 '19 at 20:29
• @JacobDenson The problem is that you use \obeycr. With \obeycr, every line end is the same as \\, which can only be used in horizontal mode (which is usually after some text has been typeset). With your definition of \subsection, after the command ends you are in vertical mode (because of \par), and then the line end makes a \\ in vertical mode, which raises the error. If you use \subsection{bar}%, then the error should go away. The correctness of this definition is debatable, though. – Phelype Oleinik Nov 30 '19 at 20:39
• @PhelypeOleinik Thank you! The question was in the close-vote queue, but it seemed that you could provide a nice answer. – Henri Menke Dec 2 '19 at 3:42

The problem you see is because you use \obeycr. The effect of \obeycr is (roughtly) the same as adding \\ at the end of every line of input, so inside your aside environment it is like if you had typed:

\section{foo}\\
bar\\
~\\
\section{foo}\\
\subsection{bar}\\


In the body of the text, under usual circumstances, the command \\ means a line break. However this command does some sanity checking and raises an error when there is no line to end, which is precisely the error you see. More specifically, when you use the command \\ in vertical mode (after a \par), the error is raised because you can't make a line break in vertical mode. A very quick way to make that error is:

\documentclass{article}
\begin{document}
\\
\end{document}


because when the document begins TeX is in vertical mode, and then you use \\. To fix the error, just type something before the \\.

You see that error with your definition because your \subsection command does \par after writing the subsection title (and the \vspace that follows doesn't change TeX's mode), and right after that there is a line end (thus a \\), and we already know what happens in this case. To work around this issue you can simply add a comment character after \subsection{bar}, then TeX will not insert the line end character.

A handier way to work around that is to add this \removecr command at the end of the definition of \subsection:

{\makeatletter\obeycr%
\gdef\removecr{\@ifnextchar^^M{\@gobblecr}{}}%
\def\@gobblecr^^M{}}


then it will look for one (only one!) line end, and remove it, if found.

Here's a compilable example:

\documentclass{article}
{\makeatletter\obeycr%
\gdef\removecr{\@ifnextchar^^M{\@gobblecr}{}}%
\def\@gobblecr^^M{}}
\newenvironment{aside}{%
\renewcommand{\section}[1]{%
\par \vspace{\baselineskip}{\LARGE ##1}}%
\renewcommand{\subsection}[1]{%
\par \vspace{.5\parskip}%
{\Large #1}%
\par \vspace{.25\parskip}\removecr}
\begin{flushright}%
\obeycr
}{\end{flushright}}
\begin{document}
\begin{aside}
\section{foo}
bar
~
\section{foo}
\subsection{bar}
\end{aside}
\end{document}


Note also that you had several unprotected line ends which were generating spurious spaces (add % at the end of the line to get rid of those). You also don't need to save the definition of \section because it is automatically restored at the end of the environment. The same goes for the effect of \obeycr.

I highly recommend you don't use \obeycr. It will make you more problems than it will solve. Also, the way you are redefining section breaks a lot of LaTeX's sectioning functionalities...

• That bit seems to be from the friggeri-cv template. I don't have a recent LaTeX installation and cannot check, if the template itself stops working. – Johannes_B Dec 2 '19 at 4:04
• @Johannes_B Indeed, the template I found in Overleaf has the same bits of code (if only OP had told the class...). The template doesn't seem to have a good support for subsections, though. The code for \subsection is roughly \def\subsection#1#2{\par\Large #2\par}, with one useless first argument and a font change that leaks to the rest of the document. Great :D – Phelype Oleinik Dec 2 '19 at 14:09