섹션 번호와 섹션 이름을 다른 색상으로 색칠하는 방법은 무엇입니까?

섹션 번호와 섹션 이름을 다른 색상으로 색칠하는 방법은 무엇입니까?

패키지 \documentclass{book}사용 시 섹션 번호와 섹션 이름에 다른 색상을 사용하고 싶습니다 . sectsty섹션 번호는 색상 상자에 입력해야 합니다. 나에게 아이디어를 주세요.

답변1

제 생각에는 sectsty이 작업에는 적합하지 않은 것 같습니다. 나는 당신에게 다음을 제안하고 싶습니다.titlesec대신 패키지로 보내세요. 간단한 예(원하는 대로 색상 조정):

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}{1em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

여기에 이미지 설명을 입력하세요

댓글에서 섹션 번호를 왼쪽 여백으로 이동해 달라는 요청이 있었습니다.

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\llap{\makebox[3em][l]{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}}}{0em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

답변2

예, 으로 이를 수행할 수 있지만 섹션 번호 인쇄를 담당하는 의 sectsty정의를 변경해야 합니다 .\@seccntformat

이렇게 하면 동일한 하위 섹션 및 하위 하위 섹션 제목에도 색상이 지정됩니다. 물론 서로 다른 색상이나 상자를 정의하는 것도 가능합니다.

Gonzalo와 동일한 설정을 사용했습니다.

\documentclass{book}

\usepackage{sectsty,xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\sectionfont{\color{sectitlecolor}}

\makeatletter
\renewcommand\@seccntformat[1]{%
  \colorbox{sectboxcolor}{\textcolor{secnumcolor}{\csname the#1\endcsname}}%
  \quad
}
\makeatother

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보