8

I am needing to create subsection and subsubsection titles that have 2 out of 4 sides bordered. One of them is a solid border, the other a dotted border.

SubSection and SubSubSection Titles

Is this possible with LaTeX? I haven't found any examples like this yet.

  • 2
    Welcome to TeX.SX. It should be possible with titlesec, but this partial borders do not really look nice, in my point of view. – user31729 Jun 2 '15 at 8:07
  • 1
    I agree, they don't that great but it is a requirement. Thanks for the tip! – ahhchuu Jun 2 '15 at 8:09
  • 1
    In case you being interested: I updated my solution and improved it. It's even easier now. – user31729 Jun 2 '15 at 22:21
11

A 'hack' of the \@sect command using tcolorbox approach.

Since tcolorbox provides a lot of configurability, there are much more possibilities.

A own tcbox designed for this would be better -- I will update my solution.

\documentclass{article}


\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\makeatletter

\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifstrequal{#1}{section}{%
    \begin{tcolorbox}[enhanced,arc=0mm,auto outer arc,bottomrule=0pt,rightrule=0pt,left=1mm,colback=white,colframe=blue]
    }{%
      \ifstrequal{#1}{subsection}{%
        \begin{tcolorbox}[enhanced,frame hidden,arc=0mm,auto outer arc,bottomrule=0pt,rightrule=0pt,left=1mm,colback=white,borderline north={0.5pt}{0pt}{blue,dotted},borderline west={0.5pt}{0pt}{blue,dotted}]
        }{}%
      }%
      \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%

    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
    \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}%
  \ifstrequal{#1}{section}{%
  \end{tcolorbox}%
  }{%
    \ifstrequal{#1}{subsection}{%
      \end{tcolorbox}{}%
    }%
  }%
}
\begin{document}

\section{First section}

\subsection{First subsection}

\subsubsection{Not framed}
\end{document}

enter image description here

Edit Further improvement, extended to all sectioning commands (except \part) and a predefined tcolorbox style box for this.

\documentclass{book}

\usepackage{xpatch}
\usepackage[most]{tcolorbox}


\newcommand\chapterstyle{sharp corners=north,bottomrule=1pt,rightrule=1pt,boxrule=5pt,arc=0.2cm,auto outer arc,frame style={left color=red!100!black,
right color=yellow!75!blue}}

\newcommand\sectionstyle{rightrule=1pt,bottomrule=1pt,drop lifted shadow={black!50!white}}%
\newcommand\subsectionstyle{frame hidden,borderline north={1pt}{0pt}{red},borderline west={1pt}{0pt}{red}}%
\newcommand\subsubsectionstyle{frame hidden,borderline north={1pt}{0pt}{blue,dotted},borderline west={1pt}{0pt}{blue,dotted}}%
\newcommand\paragraphstyle{frame hidden, borderline north={2pt}{0pt}{violet,loosely dashed},borderline west={2pt}{0pt}{violet,loosely dotted}}%
\newcommand\subparagraphstyle{colframe=brown,boxrule=2pt}%


\newtcolorbox{tsectionheadingbox}[1][]{enhanced,arc=0mm,auto outer arc,bottomrule=0pt,rightrule=0pt,left=1mm,colback=white,colframe=blue,#1}%

\newif\ifclosebox
\closeboxfalse
\makeatletter

\newcommand{\@sectioncmdendhook}[1]{%
  \ifclosebox
    \end{tsectionheadingbox}%
 \fi
}

\xpatchcmd{\@makechapterhead}{%
  \parindent \z@ \raggedright \normalfont
}{%
  \parindent \z@ \raggedright \normalfont
  \@sectioncmdstarthook{chapter}%
}{\typeout{patching non-mainmatter chapter}}{}

\xpatchcmd{\@makechapterhead}{%
  \Huge \bfseries #1\par\nobreak
  \vskip 40\p@
}{%
  \Huge \bfseries #1\par\nobreak
  \vskip 40\p@
  \@sectioncmdendhook{chapter}%
}{\typeout{patching mainmatter chapter}}{} 


\newcommand{\@sectioncmdstarthook}[1]{%
  \ifcsdef{#1style}{%
    \typeout{enabling for #1}
    \global\closeboxtrue%
    \edef\x{\csname #1style\endcsname}%
    \begin{tsectionheadingbox}[code={\pgfkeysalsofrom\x}]
    }{}%
}


\xpatchcmd{\@sect}{%
  \begingroup
  #6{%
    \@hangfrom{\hskip #3\relax\@svsec}%
    \interlinepenalty \@M #8\@@par}%
  \endgroup
}{%
  \begingroup
  \@sectioncmdstarthook{#1}%
  #6{%
    \@hangfrom{\hskip #3\relax\@svsec}%
    \interlinepenalty \@M #8\@@par}%
  \@sectioncmdendhook{#1}%
  \endgroup
}{\typeout{Success}}{\typeout{Failed}}

\xpatchcmd{\@sect}{%
      #6{\hskip #3\relax
      \@svsec #8}%
}{%
  \@sectioncmdstarthook{#1}%
  #6{\hskip #3\relax
    \@svsec #8}%
  \@sectioncmdendhook{#1}%
}{}{}


\setcounter{secnumdepth}{6}

\begin{document}
\tableofcontents

\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\paragraph{First paragraph}
\subparagraph{First subparagraph}
\end{document}

enter image description here

|improve this answer|||||

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.