4

I'd like to write a document so that \section material shows up in the margin and so that \subsection material shows up on the same line as the \section in the margin (assuming there isn't anything else in between the \section and \subsection commands, of course).

Roughly, I'd like something that looks like this:

desired output

The following code (with the \vspace command uncommented) produces the output above.

However, this implementation seems a bit hacky since you have to put arbitrary \vpsace's after each \subsection that isn't the first one beneath a \section command and after each \subsection that is the first \subsection in a \section but is preceded by other content.

What would be a better and more robust way to do this?

\documentclass{article}

\usepackage{parskip}
\usepackage{enumitem}
\setlist{nosep}

\usepackage{titlesec}

\titleformat{\section}[leftmargin]
  {\Large\scshape\raggedright}
  {\thesection}
  {1em}
  {}

\titlespacing{\section}
  {7pc}
  {1ex plus .1ex minus .2ex}
  {0pc}

\titleformat{\subsection}
  {\large\scshape\raggedright}
  {\thesubsection}
  {1em}
  {}

\titlespacing{\subsection}
  {0pt}
  {-1.6\baselineskip}
  {0pt}

\begin{document}

\section*{asdf}

\subsection*{qwerty}

\begin{itemize}
  \item{poiu}
\end{itemize}

%\vspace{1.6\baselineskip}
\subsection*{zxcv}

\begin{itemize}
  \item{lkjh}
\end{itemize}

\section*{mnbv}

lorem ipsum

%\vspace{1.6\baselineskip}
\subsection*{zqpm}

\end{document}
3

Here's a slightly less hacky way. I've created a conditional using etoolbox to align subsections with sections if they immediately follow a section but not otherwise.

The only real hack here is that if you have a section which is not immediately followed by a subsection, you need to explicitly add a \nosubsection command after it.

Since you're not numbering sections or subsections I've just set secnumdepth to 0 rather than using \section* etc. which are not then necessary.

\documentclass{article}

\usepackage{parskip}
\usepackage{enumitem}
\usepackage{etoolbox}
\setlist{nosep}
\newbool{subsection}
\newcommand*{\nosubsection}{\global\boolfalse{subsection}}
\usepackage{titlesec}
\setcounter{secnumdepth}{0}

\titleformat{\section}[leftmargin]
  {\Large\scshape\raggedright}
  {\thesection}
  {1em}
  {\global\booltrue{subsection}}

\titlespacing{\section}
  {1in}
  {1ex plus .1ex minus .2ex}
  {1em}

\titleformat{\subsection}
  {\ifbool{subsection}{\vspace{-1.6\baselineskip}\nosubsection}{}\large\scshape\raggedright}
  {\thesubsection}
  {1em}
  {}

\titlespacing{\subsection}
  {0pt}
  {0pt}
  {0pt}


\begin{document}

\section{Section}
\subsection{Subsection}
\begin{itemize}
  \item{item}
\end{itemize}

\subsection{Subsection}

Some text

\begin{itemize}
  \item{item}
\end{itemize}

\section{Section}
\nosubsection
lorem ipsum

\subsection{Subsection}

\end{document}

output of code

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.