为 \section & \subsection 命令定义不同的样式

为 \section & \subsection 命令定义不同的样式

我正在尝试自定义我的\section\subsection命令,使它们具有两种不同的样式。使用以下代码,我能够在部分编号周围绘制方形阴影。

    \documentclass[a4paper,9pt]{extarticle}
    \usepackage[utf8]{inputenc}
    \usepackage{helvet}
      \renewcommand{\familydefault}{\sfdefault}
    \usepackage{titlesec}
      \titleformat*{\section}{\LARGE\mdseries}
      \titleformat*{\subsection}{\Large\bfseries}
    \usepackage{color}
    \makeatletter
      \definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
      \let\origsection\section 
      \let\origsubsection\subsection
      \def\thesectiontext{\@arabic\c@section.}
      \def\thesubsectiontext{\thesectiontext\@arabic\c@subsection}
      \global\newsavebox{\sectbox}
      \def\makesectbox#1{\savebox\sectbox{\colorbox{sectcolor}
          {\makebox[1.47cm][r]{\normalfont\large\bfseries#1}}}}
      \renewcommand \thesection {\usebox{\sectbox}}
      \renewcommand \thesubsection {\usebox{\sectbox}}
      \renewcommand\section{\@ifstar{\myheading}{\mysection}}
      \newcommand\mysection[1]{\sectprelude{section}%
          {\thesectiontext}\origsection{#1}\mdseries}
      \titleformat{\mysection}
          {\fontsize{16}{19}\selectfont}{\thesection}{1em}{}
      \newcommand\myheading[1]{\sectprelude{section}%
          {\thesectiontext}\origsection*{#1}}
      \renewcommand\subsection[1]{\sectprelude{subsection}%
          {\thesubsectiontext}\origsubsection{#1}}
      \newcommand\sectprelude[2]{%
          \addtocounter{#1}{1}\makesectbox{#2}\addtocounter{#1}{-1}}
    \makeatother 
    \makeatletter
      \def\@seccntformat#1{\llap{\csname the#1\endcsname\quad}} 
    \makeatother 


    \begin{document}

    \section{Neuromancer}
    \vspace{1em}
     Count Zero Interrupt ...
    \section{Foundation series}

    \subsection{Foundation}
    \subsection{Foundation and Empire}
    \end{document}

该代码的结果可以在这里看到:在此处输入图片描述

但是我希望编号周围没有阴影框\subsection,并且命令的节号\section与命令的节号对齐\subsection(意味着点左侧的所有 2 具有相同的水平坐标)

有人知道解决办法吗?

答案1

这是一个选项:

\documentclass[a4paper,9pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\usepackage{xcolor}
\usepackage{titlesec}

\renewcommand{\familydefault}{\sfdefault}
\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}

\titleformat{\section}
  {\normalfont\LARGE\selectfont\bfseries}
  {\llap{\colorbox{sectcolor}{%
    \makebox[1.4cm][r]{%
      \makebox[0.7cm][l]{\thesection.\hfill}}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}
\titleformat{\subsection}
  {\normalfont\Large\bfseries}
  {\llap{\colorbox{white}{%
    \makebox[1.4cm][r]{%
      \makebox[0.7cm][l]{\thesubsection\hfill}}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}

\begin{document}

\section{Neuromancer}
Count Zero Interrupt ...
\section{Foundation series}
\subsection{Foundation}
\subsection{Foundation and Empire}

\end{document}

在此处输入图片描述

改用

\titleformat{\section}
  {\normalfont\LARGE\selectfont\bfseries}
  {\llap{\colorbox{sectcolor}{%
    \makebox[1.4cm][l]{%
      \thesection.}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}
\titleformat{\subsection}
  {\normalfont\Large\bfseries}
  {\llap{\colorbox{white}{%
    \makebox[1.4cm][l]{%
      \thesubsection}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}

你得到的颜色框内向左刷新的数字:

在此处输入图片描述

相关内容