具有图形格式的章节

具有图形格式的章节

我正在格式化一本书,我正在使用 LuaLaTex。我试图为章节获取以下格式, 复制章节编号和标题

以下代码正确生成章节标题

\usepackage{titlesec}

%Chapter heading
\newfontfamily\chapterfont[Color=black]{Avenir Light}% set font

\titlespacing{\chapter}% {left}{before}{after}[right]
{50pt}%left
{3ex plus 2ex}%before
{1.7cm}%after

\titleformat{\chapter}% command to be modified
  {\fontsize{24}{27}\bfseries\scshape\chapterfont} %format
  {\thechapter.}% label
  {1em}% sep
  {} % before-code
  [{\color{red}\titlerule[1pt]}] % after-code

但我找不到制作图形章节编号的方法,也无法将红色规则延伸到右页边框。任何帮助都将不胜感激。提前谢谢您。

答案1

基于tcolorbox包的解决方案


% !TeX program = lualatex                                   
% !TeX encoding = utf8
\documentclass[14pt]{book}
\usepackage{fontspec}
\usepackage[svgnames]{xcolor}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[many]{tcolorbox}
\usepackage[
    a4paper,
    left=2cm,
    right=2cm,
    top=2.5cm,
    bottom=2cm,
]{geometry}

\newtcbox{\mychapternum}{enhanced,nobeforeafter,tcbox raise base,boxrule=1pt,top=0mm,bottom=0mm,
  colframe=cyan,coltext=cyan,colback=cyan!10!white, halign = right,
    width=50pt,arc=7mm, sharp corners=west, boxsep=4mm,
    overlay={\draw[thick] ([yshift=3ex]frame.north west) -- ([yshift=-3ex]frame.south west);

      }
  }  

%Chapter heading
%\newfontfamily\chapterfont[Color=black]{Avenir Light}% set font
\titlespacing{\chapter}% {left}{before}{after}[right]
{60pt}%left
{1ex plus 2ex}%before
{1.7cm}%after

\titleformat{\chapter}% command to be modified
  {\fontsize{24}{27}\bfseries\scshape} %format
  {\hspace*{-70pt}
    \mychapternum{\thechapter}
    }% label
  {0em}% sep
  {} % before-code
  [{\color{red}
    \begingroup
      \setlength{\titlewidth}{\textwidth}
      \titleline*{\titlerule[1pt]}
    \endgroup  
    }] % after-code

\begin{document}


\chapter{My title}

\lipsum[20-30]

\chapter{My title}

\lipsum[20-30]
\end{document}

在此处输入图片描述

基于TikZ包的解决方案。该解决方案调整起来更加灵活。


% !TeX program = lualatex                                   
% !TeX encoding = utf8    
\documentclass[14pt]{book}      
\usepackage{fontspec}      
\usepackage[svgnames]{xcolor}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[many]{tcolorbox}
\usetikzlibrary{shapes.misc, positioning}
\usetikzlibrary{calc}   
\usepackage[
    a4paper,
    left=2cm,
    right=2cm,
    top=2.5cm,
    bottom=2cm,
]{geometry}   
%Chapter heading
%\newfontfamily\chapterfont[Color=black]{Avenir Light}% set font
\titlespacing{\chapter}% {left}{before}{after}[right]
{2cm}%left
{2ex}%before
{1.7cm}%after

\titleformat{\chapter}% command to be modified
  {\Huge\bfseries\scshape} %format
  {\hspace*{-1cm}
      \tikz [anchor=base, baseline,remember picture, overlay]{%
        \node[line width = 2pt, draw=cyan, fill =cyan!10, anchor=base, rectangle,  text width=2cm, align=right, rounded rectangle,rounded rectangle west arc=5pt, minimum height=4ex,
           inner sep=11pt] (A)
              {\color{cyan}\thechapter};
    \draw[line width = 2.5pt] ([yshift=3ex]A.north west) -- ([yshift=-3ex]A.south west);
    \draw[line width = 1pt, red] let \p1=(A.south east), \p2=(current page.east) in  ([xshift=2.6ex]A.south east) -- +(\x2,0);
    }%
    }% label
  {\dimexpr2em\relax}% sep
  {} % before-code
  [] % after-code

\begin{document}
\chapter{My title}
\lipsum[20-30]
\addtocounter{chapter}{20}
\chapter{My title}
\lipsum[20-30]
\end{document}

在此处输入图片描述

相关内容