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...
\section
as you are doing...)\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.