答案1
您可以使用titlesec
和tcolorbox
;有两个命令\FramedSections
和,\RegularSections
可以根据需要在文档中多次使用,以切换到框架或非框架子部分:
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage[explicit]{titlesec}
\renewcommand\thesubsection{\arabic{subsection}}
\newcommand\RegularSections{
\titleformat{\subsection}
{\normalfont\large\bfseries}{\thesubsection.}{1em}{##1}
}
\newcommand\FramedSections{
\titleformat{\subsection}
{\normalfont\large\bfseries}
{}
{0em}
{\begin{tcolorbox}[oversize,colback=white,left=1pt,right=1pt,nobeforeafter]\thesubsection. ##1\end{tcolorbox}}
\titleformat{name=\subsection,numberless}
{\normalfont\large\bfseries}
{}
{0em}
{\begin{tcolorbox}[oversize,colback=white,left=1pt,right=1pt,nobeforeafter]##1\end{tcolorbox}}
}
\titlespacing*{\subsection}
{0pt}{1.5ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\begin{document}
\subsection{A test numbered regular section title}
Some test text
\FramedSections
\subsection{A test numbered framed section title}
Some test text
\subsection*{A test unnumbered framed section title}
Some test text
\RegularSections
\subsection{A test numbered regular section title}
\end{document}
更新
如果您需要的是一个带有一些文本的带框编号框,与分段单元无关。那么您可以简单地使用tcolorbox
它来构建框:
\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox[auto counter]{MyTCBFramedTitle}{
oversize,
colback=white,
left=1pt,
right=1pt,
fontupper=\large\bfseries,
}
\newcommand\MyFramedTitle[1]{%
\begin{MyTCBFramedTitle}
\thetcbcounter.\hspace{0.5em}#1
\end{MyTCBFramedTitle}%
}
\begin{document}
\section{A test numbered section title}
Some test text
\MyFramedTitle{A test framed title}
Some test text
\MyFramedTitle{Another test framed title}
Some test text
\section{Another test numbered section title}
Some test text
\MyFramedTitle{Yet another test framed title}
\end{document}