使用 KOMA-Script 在未編號部分標題前面新增項目符號

使用 KOMA-Script 在未編號部分標題前面新增項目符號

我試圖在文件中所有部分的頁邊空白處添加項目符號,但 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]

答案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}

輸出:

在此輸入影像描述

等待更好的答案,因為我並不真正使用這個文檔類

相關內容