\titleformat 有多个参数吗?

\titleformat 有多个参数吗?

我使用titlesec带有\titleformat命令的包在我的文档中创建一种\part样式。但是我需要为背景图像传递一个额外的参数。截至目前,它被指定为backgroundimage,并且对于文档中的所有都是相同的\part。但我想将其作为参数发送,如下所示:\part{This is a part}{background}

似乎无法使用 \titleformat 命令添加额外参数。有人能帮我找到其他方法吗?

这就是我现在所拥有的:

\documentclass[12pt]{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}

\titleformat{\part}
  {\normalfont\huge\filleft}
  {}
  {20pt}
  {\begin{tikzpicture}[remember picture,overlay]
  \node[inner sep=0pt] (background) at (current page.center) {\includegraphics[width=\paperwidth]{backgroundimage}}; % bg-image
  \node[ % top block
      fill=blue,
      opacity=0.8,
      text width=2\paperwidth,
      rounded corners=0cm,
      text depth=18cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (parttop)
    {\thepart};% part number
  \node[
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (partnum) at ([yshift=60pt, xshift=-20pt]parttop.south)
    {\thepart};
  \node[ % name
      anchor=south east,
      color=white,
      inner sep=0pt,
      outer sep=0pt] at ([yshift=30pt, xshift=-20pt]parttop.south) 
  {\parbox{1.7\textwidth}{\raggedleft#1}};
  \end{tikzpicture}%
  }
\begin{document}


\part{This is a part}
bla bla bla

\end{document}

答案1

您可以使用单独的宏来存储背景,然后part在打印部分之前修补以设置此宏:

\documentclass[12pt]{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}

\newcommand\partbackground{}
\titleformat{\part}
  {\normalfont\huge\filleft}
  {}
  {20pt}
  {\begin{tikzpicture}[remember picture,overlay]
  \node[inner sep=0pt] (background) at (current page.center) {\includegraphics[width=\paperwidth]{\partbackground}}; % bg-image
  \node[ % top block
      fill=blue,
      opacity=0.8,
      text width=2\paperwidth,
      rounded corners=0cm,
      text depth=18cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (parttop)
    {\thepart};% part number
  \node[
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (partnum) at ([yshift=60pt, xshift=-20pt]parttop.south)
    {\thepart};
  \node[ % name
      anchor=south east,
      color=white,
      inner sep=0pt,
      outer sep=0pt] at ([yshift=30pt, xshift=-20pt]parttop.south) 
  {\parbox{1.7\textwidth}{\raggedleft#1}};
  \end{tikzpicture}%
  }
\let\origpart\part
\renewcommand\part[2]{\edef\partbackground{#2}\origpart{#1}}
\begin{document}


\part{This is a part}{some_bg.png}
bla bla bla

\end{document}

答案2

我会避免超载\part;而且我发现最好不要\titleformat用长代码填充最后一个参数。

\documentclass[12pt]{book}
\usepackage{titlesec}
\usepackage{tikz}

\titleformat{\part}
  {\normalfont\huge\filleft}
  {}
  {20pt}
  {\makepartbackground}

\newcommand{\makepartbackground}[1]{%
  \begin{tikzpicture}[remember picture,overlay]
    \if\relax\currentpartbackgroundimage\relax
    \else
      \node[inner sep=0pt] (background) at (current page.center) {%
        \includegraphics[width=\paperwidth]{\currentpartbackgroundimage}%
      }; % bg-image
    \fi
  \node[ % top block
      fill=blue,
      opacity=0.8,
      text width=2\paperwidth,
      rounded corners=0cm,
      text depth=18cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (parttop)
    {\thepart};% part number
  \node[
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (partnum) at ([yshift=60pt, xshift=-20pt]parttop.south)
    {\thepart};
  \node[ % name
      anchor=south east,
      color=white,
      inner sep=0pt,
      outer sep=0pt] at ([yshift=30pt, xshift=-20pt]parttop.south) 
  {\parbox{1.7\textwidth}{\raggedleft#1}};
  \end{tikzpicture}%
}
\newcommand{\partbackground}[1]{\gdef\currentpartbackgroundimage{#1}}
\newcommand{\nopartbackground}{\gdef\currentpartbackgroundimage{}}
\nopartbackground

\begin{document}

\part{No background}

\partbackground{example-image}
\part{This is a part}

\end{document}

所选的背景图像将一直使用,直到被重置\nobackground或被另一个\partbackground命令(在相应的之前使用\part)为止。

在此处输入图片描述

相关内容