我试图在文档中所有章节的边距中添加一个项目符号,但 KOMA 命令似乎只影响编号章节,而不影响未编号章节。我是不是漏掉了什么?(最后所有章节都将取消编号,只添加一个项目符号。)
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\renewcommand*{\sectionformat}{%
\makebox[0pt][r]{\textcolor{gray}{\textbullet}~}}
\begin{document}
\section{Section}
\section*{Unnumbered section}
\end{document}
答案1
你可以重新定义\sectionlinesformat
:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#1}{section}{\makebox[0pt][r]{\normalfont\textcolor{gray}{\textbullet}~}}%
\@hangfrom{\hskip #2#3}{#4}% original definition
}
\makeatother
\usepackage{lipsum}% only for dummy text
\begin{document}
\section{Section}
\lipsum[1]
\addsec*{Unnumbered section}
\lipsum[2]
\end{document}
或者你可以使用以下命令:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{xpatch}
\xpretocmd\sectionlinesformat
{\ifstr{#1}{section}{\makebox[0pt][r]{\normalfont\textcolor{gray}{\textbullet}~}}}
{}{\PatchFailed}
\usepackage{lipsum}% only for dummy text
\begin{document}
\section{Section}
\lipsum[1]
\addsec*{Unnumbered section}
\lipsum[2]
\end{document}
结果和上面一样。
答案2
从我的一个旧答案中重新定义了部分(如果符合您的需要,可以进行改进,因为它可能会导致“toc 部分”出现问题等):
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
%\renewcommand*{\sectionformat}{%
%\makebox[0pt][r]{\textcolor{gray}{\textbullet}~}}
\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\def\thesection{~}\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\setkomafont{section}{\sectionformat}%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}
\def\@StarredWith[#1]#2{%
\oldsection*{\makebox[0pt][r]{\textcolor{gray}{\textbullet}~}#2}%
}
\def\@StarredWithout#1{
\oldsection*{\makebox[0pt][r]{\textcolor{gray}{\textbullet}~}#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{\textcolor{gray}{\textbullet}~#2}%
}
\def\@nonStarredWithout#1{%
\oldsection{\textcolor{gray}{\textbullet}~#1}%
}
\makeatother
\begin{document}
\section{Section}
\section*{Unnumbered section}
\end{document}
输出:
等待更好的答案,因为我并不真正使用这个文档类