[Tex/LaTex] Managing spacing around \section inside a minipage with titlesec

minipagesectioningtitlesec

I'm having some trouble getting proper padding around my section headers inside minipages.

\documentclass{article}
\usepackage[explicit]{titlesec}

\newcommand\Vhrulefill{\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}
\titleformat{\section}{\bfseries\large}{}{0pt}{\noindent\Vhrulefill~#1~\Vhrulefill}

\begin{document}
    \begin{minipage}[t]{0.45\textwidth}
        \section{Introduction}
            Some content for the first section
        \section{One More Section}
            \begin{itemize}
                \item Item 1
                \item Item 2
            \end{itemize}
        \section{Another Section}
            Some text outside of a list here
        \section{Last section}
            \begin{itemize}
                \item One here
                \item One more
            \end{itemize}
    \end{minipage}%
    \hfill\vline\hfill
    \begin{minipage}[t]{0.45\textwidth}
        \section{Introduction}
            Some content for the first section
        \section{One More Section}
            \begin{itemize}
                \item Item 1
                \item Item 2
            \end{itemize}
        \section{Another Section}
            Some text outside of a list here
        \section{Last section}
            \begin{itemize}
                \item One here
                \item One more
            \end{itemize}
    \end{minipage}%
\end{document}

demo

There's too little spacing before "One More Section" and too much after.

When I try to adjust it with \titlespacing, it doesn't affect all the sections

Best Answer

Here are 3 solutions

  1. Using multicols environment from multicol package enter image description here

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{multicol}

\newcommand\Vhrulefill{\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}
\titleformat{\section}{\bfseries\large}{}{0pt}{\noindent\Vhrulefill~#1~\Vhrulefill}

\setlength\columnseprule{.4pt}
%\setlength\columnsep{12pt}
\begin{document}
\begin{multicols}{2}
\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}

%\columnbreak

\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}
\end{multicols}
\end{document}
  1. Using parbox command enter image description here

\documentclass{article}
\usepackage[explicit]{titlesec}

\newcommand\Vhrulefill{\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}
\titleformat{\section}{\bfseries\large}{}{0pt}{\noindent\Vhrulefill~#1~\Vhrulefill}

\begin{document}
\parbox[t]{0.45\textwidth}{%
\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}
}%
\hfill\vline\hfill
\parbox[t]{0.45\textwidth}{%
\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}
}
\end{document}
  1. A work around minipage it seems that we can't start a minipage with \section so we used \ (space) or ~ enter image description here

\documentclass{article}
\usepackage[explicit]{titlesec}

\newcommand*\thirdoffive[5]{#3}
\makeatletter
\newcommand*\mt{\vskip-\expandafter\thirdoffive\ttls@section\ }
\makeatother
\newcommand\Vhrulefill{\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}
\titleformat{\section}{\bfseries\large}{}{0pt}{\noindent\Vhrulefill~#1~\Vhrulefill}

\begin{document}
\begin{minipage}[t]{0.45\textwidth}\mt
\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}
\end{minipage}%
\hfill\vline\hfill
\begin{minipage}[t]{0.45\textwidth}\mt
\section{Introduction}
 Some content for the first section
\section{One More Section}
\begin{itemize}
   \item Item 1
   \item Item 2
\end{itemize}
\section{Another Section}
Some text outside of a list here
\section{Last section}
\begin{itemize}
    \item One here
    \item One more
\end{itemize}
\end{minipage}
\end{document}
Related Question