在节标题前放置一个栏目/图形

在节标题前放置一个栏目/图形

我使用了 titlesec 包来生成如下章节标题:

截屏
单击可查看全分辨率版本。

使用这个最少的代码:

\documentclass{article}
\usepackage[compact]{titlesec}
\usepackage{blindtext}
\usepackage{tikz}\usetikzlibrary{shapes.misc}

\newcommand\secbar {
    \tikz[baseline, trim left=3.2cm] 
    {
        \fill [white] (3cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \draw [gray!95, fill=gray!80] (0cm,0.1ex) rectangle (3cm,1.1ex);        
    }
}
\newcommand\subsecbar {
    \tikz[baseline, trim left=2.2cm] 
    {
        \fill [white] (2cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \fill [blue!40] (0cm,0.1ex) rectangle (2cm,1.1ex);      
    }

\titleformat{\section}{\large}{}{0cm}{\secbar}
\titleformat{\subsection}{\large}{}{0cm}{\subsecbar}

\begin{document}
\section{A section}
\subsection{A subsection}
\end{document}

但是,我想使用 memoir 包,它与 titlesec 不兼容。我知道 memoir 有自己的命令来更改部分。例如,\setsechook{Hello}将在部分标题前放置“Hello”。但是,它还会在“Hello”后放置一个换行符。查看 memoir 文档时,我还没有取得很大进展。

有人可以指导我重现与上面相同的节标题命令,但使用 memoir 包而不使用 titlesec 吗?

答案1

一种可能性是:

\documentclass{memoir}
\usepackage{tikz}

\setcounter{secnumdepth}{0}

\newcommand\secbar{%
    \tikz[baseline, trim left=3.2cm] 
    {
        \fill [white] (3cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \draw [gray!95, fill=gray!80] (0cm,0.1ex) rectangle (3cm,1.1ex);        
    }%
}
\newcommand\subsecbar{%
    \tikz[baseline, trim left=2.2cm] 
    {
        \fill [white] (2cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \fill [blue!40] (0cm,0.1ex) rectangle (2cm,1.1ex);      
    }%
}
\makeatletter
\renewcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\noindent\llap{\secbar}\normalfont\secheadstyle}} % font
\setsecheadstyle{\large\memRTLraggedright}
\renewcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\noindent\llap{\subsecbar}\normalfont\subsecheadstyle}} % font
\setsubsecheadstyle{\large\memRTLraggedright}
\makeatother

\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

在此处输入图片描述

顺便说一句,你实际上并不需要 TikZ 来绘制彩色条:

\documentclass{memoir}
\usepackage{xcolor}

\setcounter{secnumdepth}{0}

\newcommand\MyBar[5]{%
  {\setlength\fboxsep{0pt}\color{#1}\fbox{\color{#2}\rule{#3}{#4}}}\hspace{#5}}

\newcommand\secbar{\MyBar{gray!95}{gray!80}{2.8cm}{1ex}{0.2cm}}
\newcommand\subsecbar{\MyBar{blue!40}{blue!40}{1.8cm}{1ex}{0.2cm}}%

\makeatletter
\renewcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\noindent\llap{\secbar}\normalfont\secheadstyle}} % font
\setsecheadstyle{\large\memRTLraggedright}
\renewcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\noindent\llap{\subsecbar}\normalfont\subsecheadstyle}} % font
\setsubsecheadstyle{\large\memRTLraggedright}
\makeatother

\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

答案2

为什么要重新编码?只需添加\leavevmode\llap{\secbar}似乎\setsecheadstyle就可以了。

\documentclass{memoir}
\usepackage{xcolor}

\setcounter{secnumdepth}{0}

\newcommand\MyBar[5]{%
{\setlength\fboxsep{0pt}\color{#1}\fbox{\color{#2}\rule{#3}{#4}}}\hspace{#5}}

\newcommand\secbar{\MyBar{gray!95}{gray!80}{2.8cm}{1ex}{0.2cm}}
\newcommand\subsecbar{\MyBar{blue!40}{blue!40}{1.8cm}{1ex}{0.2cm}}%


\setsecheadstyle{\large\memRTLraggedright\leavevmode\llap{\secbar}}
\setsubsecheadstyle{\large\memRTLraggedright\leavevmode\llap{\subsecbar}}




\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

相关内容