![简单粗暴的解决方案基于\fbox](https://linux22.com/image/393356/%E7%AE%80%E5%8D%95%E7%B2%97%E6%9A%B4%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E5%9F%BA%E4%BA%8E%5Cfbox.png)
我想要精确的此部分样式,但无法在其周围添加框架。你能帮帮我吗?这是我的代码片段:
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage{titlesec, blindtext, color}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}[hang]{\Large\bfseries}{\thesection\hsp\textcolor{seccolor}{|}\hsp}{0pt}{\Large\bfseries}
\begin{document}
\section{Section Name}
\blindtext
\end{document}
答案1
你可以和 Ti 一起拥有它钾Z 和explicit
选项titlesec
:
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
[hang]
{\Large\bfseries}
{}
{0pt}
{\tikz\node[draw]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{|}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\end{document}
对于很长的章节标题(借助这个问题):
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\tikzset{
max width/.style={
execute at begin node={\begin{varwidth}{#1}},
execute at end node={\end{varwidth}}
}
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
[hang]
{\Large\bfseries}
{}
{0pt}
{\tikz\node[draw,max width=\textwidth-.6666em-.4pt]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{|}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\section{This is a very long section name that has to be broken into two lines}
\end{document}
如果希望用“|”代替“—”,如附图所示,可以使用\textbar
:
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\tikzset{
max width/.style={
execute at begin node={\begin{varwidth}{#1}},
execute at end node={\end{varwidth}}
}
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
[hang]
{\Large\bfseries}
{}
{0pt}
{\tikz\node[draw,max width=\textwidth-.6666em-.4pt]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{\textbar}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\end{document}
不过,我认为像这样的垂直线要好得多。
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\usetikzlibrary{positioning}
\tikzset{
max width/.style={
execute at begin node={\begin{varwidth}{#1}},
execute at end node={\end{varwidth}}
}
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
[hang]
{\Large\bfseries}
{}
{0pt}
{\tikz[font=\Large\bfseries]{\node[draw,minimum height=.75cm] (x) {\thesection};\node[minimum height=.75cm,below right=0pt and 0pt of x.north west,draw,max width=\textwidth-.6666em-.4pt]{\hspace{.3333em}\phantom{\thesection}\hspace{.3333em}#1};}}
\begin{document}
\section{Section Name}
\blindtext
\section{This is a very long section name that has to be broken into two lines}
\end{document}
答案2
一个简单的解决方案tabulary
:
\documentclass[10pt, a4paper,svgnames, twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[showframe]{geometry}
\usepackage{blindtext}
\usepackage{array, tabulary}
\usepackage[explicit]{titlesec}
\usepackage{blindtext, xcolor}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}[block]{\Large\bfseries\sffamily\setlength{\fboxrule}{1pt}\color{SlateGrey}}{}{0pt}{\fbox{\begin{tabulary}{\dimexpr\linewidth-\tabcolsep-\fboxrule}{@{}l!{\vline width 1.2pt}L}\thesection  \end{tabulary}} }
\begin{document}
\setcounter{chapter}{2}
\section{Section Title. Some more more text to have a really very very long section title.}
\blindtext
\section{A much shorter section title}
\blindtext
\end{document}
答案3
这篇文章包含两个解决方案:一个简单的解决方案和一个更复杂的解决方案。
简单粗暴的解决方案基于\fbox
让我们从基于 的简单解决方案开始\fbox
,这很容易理解。我们使用\textbar
表示垂直条,并为未编号部分提供变体(\section*
):
\documentclass[10pt,a4paper,twoside]{report}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\newcommand*{\sectionFont}{%
\Large\bfseries
}
% For \section
\titleformat{\section}[block]{\sectionFont}{}{0pt}{%
\fbox{\thesection \hsp \textcolor{seccolor}{\textbar}\hsp #1}}
% For \section*
\titleformat{name=\section, numberless}[block]{\sectionFont}{}{0pt}{%
\fbox{#1}}
\begin{document}
\chapter{Some chapter}
\section{Section title}
Foo bar.
\section*{Unnumbered section}
\blindtext
\end{document}
使用 TikZ 的更精细的解决方案
\fbox
如果您想要比(\fboxsep
和\fboxrule
)允许的更精细的控制,那么使用tikzpicture
框架的环境是一个不错的选择。下面是可以这样做的示例。它实现了与以下类似的设计JouleV 的解决方案但尝试进行更清晰的定位,以确保节号的基线始终与节图块第一行的基线对齐。由于我们拥有 TikZ 的所有功能,我们还添加了一些只需几个关键字即可实现的花哨功能:rounded corners
,drop shadow
以及标题框的背景填充。
(再多一点黑客技术,甚至可以检测到何时章节标题占用多行,并且只有在这种情况下才使用垂直居中来显示章节编号!)
\documentclass[a4paper]{report}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{etoolbox}
\usepackage{varwidth}
\usepackage{calc}
\usepackage{blindtext}
\usepackage{tikz}
% Uncomment the following line if you use babel and have a recent enough TikZ:
% \usetikzlibrary{babel}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{shadows}
% Essentially taken from <https://tex.stackexchange.com/a/482926/73317>
\tikzset{
max width/.style={
execute at begin node={\begin{varwidth}[t]{#1}},
execute at end node={\end{varwidth}}
}
}
% Colors used for decorated section titles
\definecolor{sectionDecoRuleColor}{RGB}{70,10,10}
\colorlet{sectionDecoBgColor}{yellow!10}
% Horizontal spacing used for decorated section titles
\newlength{\sectionDecoHsp}
\setlength{\sectionDecoHsp}{8pt}
\newcommand*{\sectionDecoRuleWidth}{0.4pt}
% Boolean flag indicating whether we are working with a \section or a \section*
\newtoggle{sectionDecoIsNumberedSec}
\makeatletter
% High-level command for section decorations. Two variants: one for \section
% and one for \section*.
\newcommand*{\sectionDecoration}{%
\@ifstar{%
\togglefalse{sectionDecoIsNumberedSec}%
\@sectionDecoration\@nil % \@nil passed instead of the section number
}{%
\toggletrue{sectionDecoIsNumberedSec}%
\@sectionDecoration
}%
}
% #1: the section number (\@nil if we are working for a \section*)
% #2: the section title
\newcommand*{\@sectionDecoration}[2]{%
\begin{tikzpicture}
% The section number
\iftoggle{sectionDecoIsNumberedSec}{% case of \section
\node[inner xsep=\sectionDecoHsp, inner ysep=0, outer sep=0,
anchor=base west]
(section number) at (0,0)
{\strut #1};
}{% case of \section*
\node[inner sep=0, outer sep=0, anchor=base west]
(section number) at (0,0)
{\strut};
}
% The section title (which may occupy several lines)
\path let \p1 = ($(section number.east)-(section number.west)$),
\n1 = {\linewidth - veclen(\p1) - 2\sectionDecoHsp} in
node[inner xsep=\sectionDecoHsp, inner ysep=0, outer sep=0,
anchor=base west, max width=\n1]
(section title) at (section number.base east)
{\strut #2% Useful for testing: \rule{\n1}{1pt}%
\strut};
\begin{scope}[on background layer]
% The frame around {section number + title}
\node[inner sep=0, outer sep=0, draw, line width=\sectionDecoRuleWidth,
rounded corners,
fit={([xshift=0.5\pgflinewidth]section number.north west)
([xshift=-0.5\pgflinewidth]section title.south east)},
color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
(frame) {};
% The vertical line between section number and section title
\iftoggle{sectionDecoIsNumberedSec}{% case of \section
\draw[color=sectionDecoRuleColor, line width=\sectionDecoRuleWidth,
line cap=butt]
([yshift=-0.5\pgflinewidth]section title.north west) --
([yshift=0.5\pgflinewidth]section title.south west);
}{% no such line in the case of \section*
}
\end{scope}
% Display key points (only useful for debugging)
% \begin{scope}[overlay]
% \path[radius=1pt, fill=red] (section number.north west) circle {}
% (section number.north east) circle {};
% \node[draw,circle,green, inner sep=1.6pt] at (section title.north west) {};
% \end{scope}
\end{tikzpicture}%
}
\makeatother
\newcommand*{\sectionTitleFont}{\Large\bfseries}
% For \section
\titleformat{\section}[block]{\sectionTitleFont}{}{0pt}{%
\sectionDecoration{\thesection}{#1}}
% For \section*
\titleformat{name=\section, numberless}[block]{\sectionTitleFont}{}{0pt}{%
\sectionDecoration*{#1}}
\begin{document}
\chapter{Some chapter}
\section{A short section title}
\section{A very very very very very very very very very very
very very very very very very very very very very
long section title}
Foo bar.
\section*{An unnumbered section}
\blindtext
\end{document}