如何在章节标题周围创建部分边框

如何在章节标题周围创建部分边框

我需要创建4 条边中有 2 条带边框的subsection标题subsubsection。其中一条是实线边框,另一条是虚线边框。

子节和子子节标题

使用 LaTeX 可以实现吗?我还没有找到类似的例子。

答案1

\@sect对使用方法的命令进行‘破解’ tcolorbox

由于tcolorbox提供了很多可配置性,因此存在更多可能性。

为此设计的自己的解决方案tcbox会更好——我会更新我的解决方案。

\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}

在此处输入图片描述

编辑进一步改进,扩展到所有分段命令(除\part)并tcolorbox为此预定义一个样式框。

\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}

在此处输入图片描述

相关内容