章节标题的背景填充

章节标题的背景填充

我正在尝试使用突出显示整个部分标题的背景

\documentclass{article}
\RequirePackage{titlesec}
\RequirePackage{charter}
\RequirePackage[hmargin=.3in, vmargin=0.1in]{geometry}
\RequirePackage{amsmath}
\RequirePackage{textcomp}
\RequirePackage{amssymb}
\RequirePackage{enumitem}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{multicol}
\RequirePackage{array}
\titleformat{\section}{\large\scshape\raggedright}{}{0em}{\colorbox{gray}}[\titlerule]
\titlespacing{\section}{0em}{0.5em}{0.25em}
\begin{document}
\section{Lobo}
(Type your content here.)
\end{document}

但它只突出显示文本。如何突出显示整行?

答案1

使用来自的代码https://tex.stackexchange.com/a/40051/36296给出

\documentclass{article}
\RequirePackage{titlesec}
\RequirePackage{charter}
\RequirePackage[hmargin=.3in, vmargin=0.1in]{geometry}
\RequirePackage{amsmath}
\RequirePackage{textcomp}
\RequirePackage{amssymb}
\RequirePackage{enumitem}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{multicol}
\RequirePackage{array}
\titleformat{\section}{\large\scshape\raggedright}{}{0em}{\colorsection}[\titlerule]

\titleformat{name=\section,numberless}{\large\scshape\raggedright}{}{0em}{\colorsectionnonumber}[\titlerule]

\titlespacing{\section}{0em}{0.5em}{0.25em}

\newcommand{\colorsection}[1]{%
    \colorbox{gray}{\parbox{\dimexpr\textwidth-2\fboxsep}{\thesection\ #1}}}

\newcommand{\colorsectionnonumber}[1]{%
    \colorbox{gray}{\parbox{\dimexpr\textwidth-2\fboxsep}{#1}}}

\begin{document}
    \section{bla}

    abc

    \section*{Lobo}
    (Type your content here.)
\end{document}

在此处输入图片描述

相关内容