将章节标题渐变(tikz)扩展到小节(koma-script)

将章节标题渐变(tikz)扩展到小节(koma-script)

我试图扩展答案这里(下面的代码)到subsection

在此处输入图片描述

\documentclass{scrbook}

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \ifstr{#1}{section}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
      {\@hangfrom{\hspace*{#2}#3}{#4}}%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\Blindtext
\end{document}

我想将其应用到章节中是相当简单的 \chapterlinesformat,我该如何重现这一点subsection

答案1

如果要将此布局用于所有部分(如标题(sectionsubsectionsubsubsection),您可以使用

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

例子

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

在此处输入图片描述

如果subsubsection需要获取默认布局,请使用

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \Ifstr{#1}{subsubsection}
      {\@hangfrom{\hspace*{#2}#3}{#4}}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}%
}
\makeatother

例子:

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \Ifstr{#1}{subsubsection}
      {\@hangfrom{\hspace*{#2}#3}{#4}}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

在此处输入图片描述


如果您想根据部分级别使用不同的颜色,则可以使用嵌套\Ifstring命令。但我会为每个级别定义一个颜色名称(颜色名称必须取决于部分级别),并将这些颜色用作left color

例子:

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\colorlet{sectionbg}{blue!20}
\colorlet{subsectionbg}{orange!20}
\colorlet{subsubsectionbg}{green!20}

\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=#1bg,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsection{Subsection}
\subsection*{Second section without number}
\subsection{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\subsubsection{A Subsubsection}

\Blindtext
\end{document}

在此处输入图片描述

相关内容