答案1
以下内容应该可以正常工作,而不会出现任何过满框警告,并且最终得到一个与文本宽度一样宽的矩形框。
与原版相比,我用 替换了\tabcolsep
。 (由于和 的2\fboxsep
默认值分别为 6pt 和 3pt,所以这应该不会有任何区别,但一旦你决定重新定义其中任何一个,这确实会有所不同。)我还减去了而不是只减去,因为表格的两边都有一条垂直线\tabcolsep
\fboxsep
2\fboxrule
\fboxrule
\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-2\fboxsep-2\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}