5 我需要自定义章节样式,想将章节标题与章节描述放在同一个页面

发布于 2024-07-12 14:32:31

image.png
请问我想在这个空白处加上章节描述应该怎么操作啊?
这是代码截图
image.png
image.png

这是详细代码:

\documentclass[11pt,a4paper,openany]{book}

\usepackage{titlesec}
\usepackage{titleps}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{shapes.geometric}
\newcommand{\chapnumfont}{%
  \fontsize{100}{100}\usefont{T1}{ptm}{b}{n}%
}

\colorlet{chapbgcolor}{gray!75}
\colorlet{chapnumcolor}{black!60}

% 定义一个新的命令来显示章节描述
% \newcommand{\chapterdescription}[1]{%
%   \begin{tikzpicture}[remember picture, overlay]
%     \node at (current page.south) [anchor=north, inner sep=0pt] {
%       \parbox{\textwidth}{\noindent\textbf{Description:} #1}
%     };
%   \end{tikzpicture}
% }

\newcommand{\chaptitlenumbered}[1]{%
\begin{tikzpicture}
   % 左半边图片
   \node at (\dimexpr 0.05\paperwidth, 6cm) [anchor=north] {
       % 使用\resizebox保持图片的纵横比
       \resizebox{\dimexpr 0.27\paperwidth\relax}{27cm}{\includegraphics{tu2.png}}
   };
   % 第一个矩形
   \fill[chapbgcolor!70,rounded corners=0pt] (0.31\linewidth,3) rectangle ++(0.7\linewidth,2.3cm); % 假设第一个矩形占据90%的页面宽度

   % 第二个矩形,起始点在第一个矩形的右侧边缘
   \fill[chapbgcolor!100,rounded corners=0pt] (0.95\linewidth,3) rectangle ++(0.08\linewidth,2.3cm); % 第二个矩形占据剩余的10%宽度
 %章节名称
  \node[
    align=left,
    anchor=south,
    inner sep=8pt,
    font=\huge\normalfont\bfseries
  ] at (0.50\linewidth,3) {\strut#1};
  \node[
    align=right,
    font=\fontsize{10}{10}\usefont{OT1}{ptm}{b}{it},
    text=chapnumcolor,
    rotate=90 
  ] at (0.99\linewidth,4.2) {Chapter \thechapter};

\end{tikzpicture}%

}

\newcommand{\chaptitleunnumbered}[1]{%
  \begin{tikzpicture}
  \fill[chapbgcolor!70,rounded corners=0pt] (0,2.3) rectangle (\linewidth,0);
  \node[
    align=right,
    anchor=south east,
    inner sep=8pt,
    font=\huge\normalfont\bfseries
  ] at (0.987\linewidth,0) {\strut#1};
  \end{tikzpicture}%
}

\titleformat{name=\chapter}
  [display]%用来定义章节标题的形状。
  {\normalfont\huge\bfseries\sffamily}%章节标题的格式设置
  {} % 章节编号的格式设置为空
  {0pt}% 章节编号前的间距
  {\chaptitlenumbered}

  \titleformat{name=\chapter,numberless}[display]
  {\normalfont\huge\bfseries\sffamily}
  {}
  {25pt}
  {\chaptitleunnumbered}
\titlespacing*{\chapter}
  {0pt}
  {-126pt}
  {33pt}

\setlength\headheight{15pt}

\begin{document}
\tableofcontents
\chapter{A chapter}



\end{document}

预期效果:
微信图片_20240712142555.jpg

查看更多

关注者
0
被浏览
446
鱼香肉丝没有鱼先生
鱼香肉丝没有鱼先生.

贴一下代码,看不懂的下面评论吧

\documentclass{book}

\usepackage[explicit]{titlesec}

\usepackage[margin = 2cm]{geometry}

\usepackage{tikz}

\makeatletter
\def\@chapter@description{}
\titleformat{name = \chapter}{}{}{0em}
{%
  \begin{tikzpicture}[remember picture, overlay, every node/.style={inner sep = 0pt, outer sep = 0pt}]
    \node[anchor = north west] at ([shift = {(2cm, -2cm)}]current page.north west) {\includegraphics[width = .2\textwidth, height = \textheight]{example-image-a}};
    \fill[gray!40] ([shift = {(2cm + .2\textwidth, -2.5cm)}]current page.north west) rectangle ++ (.8\textwidth, -4cm);
    \node[anchor = south west, inner sep = 10pt, scale = 2, font = \bfseries] at ([shift = {(2cm + .2\textwidth, -6.5cm)}]current page.north west) {#1};
    \fill[gray!70] ([shift = {(-2, -2.5)}]current page.north east) rectangle ++ (-1.5, -4);
    \node[rotate = 90, font=\bfseries\Large] at ([shift = {(-2.75, -4.5)}]current page.north east) {\chaptername\ \thechapter};
    \node[anchor = north west, inner sep = 10pt] at ([shift = {(2cm + .2\textwidth, -6.5cm)}]current page.north west) {
      \begin{minipage}{\dimexpr .8\textwidth - 10pt\relax}
        \parindent=2em
        \@chapter@description
      \end{minipage}
    };
  \end{tikzpicture}
}

\NewDocumentEnvironment{fancychapter}{m+b}{%
  \def\@chapter@description{#2}
  \chapter{#1}
  \newpage
}{}
\makeatother
\begin{document}


\begin{fancychapter}{Introduction \LaTeX}
  \foreach \x in {1, ..., 100} {Some test text. }
  
  \begin{itemize}
    \item Some test text.
    \item Some test text.
    \item Some test text.
    \item Some test text.
  \end{itemize}
\end{fancychapter}

\end{document}
1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览