我想用 koma 脚本为章节、小节和小子节定义自己的风格(我知道如何用 titlesec 来做,但我需要“koma 的版本”)。
我成功完成了部分和子部分的工作,但子子部分的工作却没有完成。我有这个:
我想要例如:
- 部分
1.1. 小节
(a)子子部分(例如以绿色表示)
这是我的代码
\documentclass[10pt]{scrreprt}%scrartcl,scrreprt,scrbook
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[]{libertine}
\usepackage[libertine]{newtxmath}
\usepackage{microtype}
\usepackage{setspace}
%\onehalfspacing
\renewcommand*\thesection{\arabic{section}.}
\renewcommand*\thesubsection{\arabic{section}.\arabic{subsection}}
\renewcommand*\thesubsubsection{\alph{subsubsection}}
\usepackage{tikz}
\setkomafont{section}{\color{red}}
\setkomafont{subsection}{\color{blue}}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#1}{section}
{\tikz\node[inner xsep=1em,inner ysep=1ex,left color=red!20,right color=white,rounded corners]
{\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
{\tikz\node[inner xsep=1em,inner ysep=1ex,left color=blue!20,right color=white,rounded corners]
{\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
}
\makeatother
\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}
答案1
您确实要在文档中使用节号而不使用章节号吗scrreprt
?
这是一个建议,假设所有使用的部分级别都sectionlinesformat
应该是彩色的:
\documentclass[10pt,
numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}
\setcounter{secnumdepth}{\subsubsectionnumdepth}
\colorlet{sectioncolor}{red}
\colorlet{subsectioncolor}{blue}
\colorlet{subsubsectioncolor}{green!80!black}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\tikz\node[
inner xsep=1em,inner ysep=1ex,
left color=#1color!20,right color=white,
rounded corners,text=#1color
]{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};
}
\makeatother
\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}
或者假设所有部分级别格式化都sectionlevelsformat
应该使用节点:
\documentclass[10pt,
numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}
\setcounter{secnumdepth}{\subsubsectionnumdepth}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}
\tikzset{
section/.style={text=red,left color=red!20,right color=white},
subsection/.style={text=blue,left color=blue!20, right color=white},
subsubsection/.style={}
}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\tikz\node[
inner xsep=1em,inner ysep=1ex,
rounded corners,
#1
]{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};
}
\makeatother
\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}
现在子部分没有颜色(如评论中所要求的):
如果每个级别都应该有不同的布局,那么您必须使用嵌套\ifstr{#1}{<section level name>}
命令:
\ifstr{#1}{section}
{<code for sections>}
{\ifstr{#1}{subsections}
{<code for subsections>}
{<code for all other levels>}
}%
例子:
\documentclass[10pt,
numbers=noenddot% <- added
]{scrreprt}%scrartcl,scrreprt,scrbook
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\renewcommand*\thesection{\arabic{section}}
\renewcommand*\sectionformat{\thesection.\enskip}
\renewcommand*\thesubsubsection{(\alph{subsubsection})}
\setcounter{secnumdepth}{\subsubsectionnumdepth}
\RedeclareSectionCommands[font=\normalsize]{section,subsection,subsubsection}
\tikzset{
sectionlinesformat/.style={
inner xsep=1em,inner ysep=1ex,
rounded corners
}
}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#1}{section}
{\tikz\node[sectionlinesformat,left color=red!20,right color=white,text=red]
{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
{\ifstr{#1}{subsection}
{\tikz\node[sectionlinesformat,left color=blue!20,right color= white,text=blue]
{\parbox{\dimexpr\textwidth-2em\relax}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
{\@hangfrom{\hspace*{#2}#3}{#4}}%
}%
}
\makeatother
\begin{document}
\section{section}
\subsection{subsection}
\subsubsection{subsubsection}
\end{document}