使用 Tikz 為章節標題疊加中的不同影像建立宏

使用 Tikz 為章節標題疊加中的不同影像建立宏

我想製作該章節的圖像疊加。到目前為止,我已經找到了這段程式碼,如果我直接在定義中指定圖像,它就可以工作:

\newcommand\chapterlabel{}
\titleformat{\chapter}
  {\gdef\chapterlabel{}
   \normalfont\sffamily\Huge\bfseries\scshape}
  {\gdef\chapterlabel{\thechapter\ }}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[below right=4.8cm and 0cm of current page.north east] (a)
      {\begin{tikzpicture}[remember picture, overlay]
        \fill[fill overzoom image=
!!!!!!!!!!!!HERE IS THE IMAGE NAME!!!!!!!!!!!!!!
]
 (current page.north west) rectangle
          (a);
        \node[below right=2.5cm and 2cm of current page.north
     west,color=blue]
              {\color{white}\chapterlabel#1};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }

但是,我不知道如何將其作為宏,即我想將“#2”放在圖片名稱中作為佔位符,然後我可以稍後傳遞自訂圖像名稱作為參數。

答案1

我不知道您希望輸出是什麼樣子,因為您沒有提供最小的工作範例,而只是提供了程式碼片段。無論如何,你應該避免築巢tikzpicture環境。

正如我在評論中建議的那樣,為什麼不創建一個自訂命令\myimagename來儲存當前圖像的名稱呢?同樣的情況也適用於章節標籤,但是,我從您的程式碼中不明白您打算如何使用它。

微量元素:

\documentclass{report}
\usepackage{titlesec, tikz, tikzfill}
\usetikzlibrary{positioning, shadows}

\newcommand\mychapterlabel{}
\newcommand\myimagename{example-image-a}
\titleformat{\chapter}
  {\normalfont\sffamily\Huge\bfseries\scshape}
  {\gdef\chapterlabel{\thechapter\ }}{0pt}
  {\begin{tikzpicture}[remember picture, overlay]
    \coordinate[below right=4.8cm and 0cm of current page.north east] (a);
    \fill[fill overzoom image=\myimagename] (current page.north west) rectangle (a);
    \node[below right=2.5cm and 2cm of current page.north west, color=blue]
        {\color{white}\chapterlabel\mychapterlabel};
   \end{tikzpicture}}

\begin{document}

\renewcommand\mychapterlabel{Foo}
\chapter{One}

\renewcommand\mychapterlabel{Bar}
\renewcommand\myimagename{example-image-b}
\chapter{Two}

\end{document}

在此輸入影像描述

相關內容