![部分标题的垂直颜色背景](https://linux22.com/image/297283/%E9%83%A8%E5%88%86%E6%A0%87%E9%A2%98%E7%9A%84%E5%9E%82%E7%9B%B4%E9%A2%9C%E8%89%B2%E8%83%8C%E6%99%AF.png)
晚安。这是我关于渐变色部分标题的问题,具有垂直长度。它的代码是,
\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[svgnames]{xcolor}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\titleformat{\section}[block]%
{\normalsize\bfseries\itshape\tikz[overlay] \shade[left color=red!20!yellow,right color=black!40!white] (0,-1ex) rectangle (\linewidth,1em);}%
{\thesection}%
{1em}%
{\color{Maroon}#1}
\begin{document}
\section{A textwidth section - OK.}
\lipsum[1]
\section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section - NOT OK.}
\lipsum[1]
\section{A section header full of \lipsum[1]}
\end{document}
但是,它没有为第二部分全部着色。相关的 HTML 页面对我来说不太合适。因此,应该使用什么简单的代码块添加了此代码?
有关的:
答案1
Caramdir 的回答可以轻松适用于您的情况。
\documentclass[svgnames]{scrbook}
\usepackage{tikz,lipsum}
\usepackage{lipsum}
\usepackage{titlesec}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\newcommand\boxedsection[1]{{%
\usekomafont{sectioning}\usekomafont{section}%
\begin{tikzpicture}[inner sep=0pt, inner ysep=0.3ex]
\node[anchor=base west] at (0,0) (counter) {\thesection};
\path let \p1 = (counter.base east) in node[anchor=base west, text width={\textwidth-\x1-0.33em},text=Maroon] (content) at ($(counter.base east)+(0.33em,0)$) {#1};
\begin{pgfonlayer}{background}
\shade[left color=red!20!yellow,right color=black!40!white] let \p1=(counter.north), \p2=(content.north) in
(0,{max(\y1,\y2)}) rectangle (content.south east);
\end{pgfonlayer}
\end{tikzpicture}%
}}
\titleformat{\section}%
{}%
{}%
{0pt}%
{\boxedsection}%
\begin{document}
\section{A textwidth section - OK.}
\lipsum[1]
\section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section - NOT OK.}
\lipsum[1]
\end{document}
如果您想修改代码以使其正常工作,请使用以下命令:
\documentclass[a4paper,twoside,openright,11pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[svgnames]{xcolor}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{tikz,showframe}
\titleformat{\section}[block]%
{\normalsize\bfseries\itshape}%
{}%
{0em}%
{\tikz\node[left color=red!20!yellow,right color=black!40!white,text=Maroon,inner xsep=0pt,outer sep=0pt,text width=\linewidth,] {\thesection \space #1};}
\begin{document}
\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Mass of this entire universe and me are not equal as I am a part of the universe}
\end{document}