请检查以下内容mwe
。
\documentclass[a4paper]{scrartcl}
\usepackage[dvipsnames, table]{xcolor}
\colorlet{sectioncolor}{blue}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\colorbox{sectioncolor}{%
\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
\raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
\begin{document}
\section{Hello}
\begin{itemize}
\item Hello
\subsection{Foo}
\end{itemize}
\end{document}
但这不是我想要的。我试图实现背景颜色应该在右侧对齐,例如
|----------------------------------|
|1 Hello___________________________|
* Hello
|------------------------------|
| 1.1 Foo______________________|
答案1
\textwidth
用。。。来代替\linewidth
:
\documentclass[a4paper]{scrartcl}
\usepackage[dvipsnames, table]{xcolor}
\colorlet{sectioncolor}{blue}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\colorbox{sectioncolor}{%
\parbox[t]{\dimexpr\linewidth-2\fboxsep\relax}{%
\raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
\begin{document}
\section{Hello}
\begin{itemize}
\item Hello \colorlet{sectioncolor}{Lavender!60}
\subsection{Foo}
\end{itemize}
\subsection{FooFoo}
\end{document}