彩色部分標題因 titlesec 的 [leftmargin] 選項而失敗

彩色部分標題因 titlesec 的 [leftmargin] 選項而失敗

我希望我的章節標題有顏色。我用\titleformat包裝裡的titlesec設定就可以了。

但是當我添加選項參數時[leftmargin],標題文字無法更改顏色。我的程式碼有問題嗎?

微量元素:

\documentclass{article}
\usepackage{titlesec,xcolor}

\begin{document}
\titleformat{\section}
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{my colored section}

\titleformat{\section}[leftmargin]% only the optional argument "leftmargin" added
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{The Color Unchanged}

\end{document}

在此輸入影像描述

答案1

解決方法:將顏色指令放在第二個和第四個參數。

\documentclass{article}
\usepackage{,xcolor, titlesec}

\begin{document}
\titleformat{\section}
{\normalfont\bfseries\color{blue}}
{\thesection.}
{1em}
{}
\section{my colored section}

\titleformat{\section}[leftmargin]% only the optional argument "leftmargin" added
{\normalfont\bfseries}
{\color{blue}\thesection.}
{1em}
{\color{blue}}
\section{The Color Unchanged}

\end{document} 

在此輸入影像描述

相關內容