![添加参数到 \section 以包含图形](https://linux22.com/image/344636/%E6%B7%BB%E5%8A%A0%E5%8F%82%E6%95%B0%E5%88%B0%20%5Csection%20%E4%BB%A5%E5%8C%85%E5%90%AB%E5%9B%BE%E5%BD%A2.png)
我正在尝试将 wordpress 博客翻译成一本书。由于每篇文章都有标题和标题图像,因此我正在寻找一种将标题图像传递给某个部分的方法。
我弄清楚了如何使用 tikz 包创建一个好看的部分,但现在我想知道如何自动传递相应的图片而不是手动传递。
类似的东西\section{title}{picture.jpg}
就很棒了。有什么建议吗?
以下是我目前得到的信息:
\documentclass[11pt, openany]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{mwe}
\usepackage{ae}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{microtype}
\usepackage{fancybox}
\usepackage{times}
\usepackage{lmodern}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{positioning}
\usetikzlibrary{shadows,calc}
\usepackage[explicit]{titlesec} %Custom Section title
\usepackage[a4paper, left=2.5cm, right=1.0cm, top=2cm, bottom=2.5cm]{geometry}
\usepackage{color}
\usepackage{hyperref}
\usepackage{array}
%%Custom Section-Titles
\newcommand*\sectionlabel{}
\titleformat{\section}
{\gdef\sectionlabel{}
\normalfont\Large\sffamily\bfseries\color{THDblue}}
{\gdef\sectionlabel{\thesection\ }}{0pt}
{%
\begin{tikzpicture}[remember picture]
\node[anchor=north west] (titelbild) at (0,0) {\includegraphics[scale=0.2]{example-image}};
\draw[color=THDblue] ($(titelbild.north east) + (15cm,-0.5cm)$) -- ($(titelbild.north east) + (0.5cm,-0.5cm)$) -- (titelbild.north east) -- (titelbild.north west) -- (titelbild.south west) --(titelbild.south east) --($(titelbild.south east) + (0.5cm,0.5cm)$) -- ($(titelbild.south east) + (15cm,0.5cm)$);
\node[anchor=north west, text width=9cm] at ($(titelbild.north east) + (0.3cm,-0.8cm)$){\sectionlabel#1};
\end{tikzpicture}
}
%-------------------------------------
\definecolor{THDblue}{rgb}{0.1000,0.4000,0.4000}
\begin{document}
\chapter{This is the titel of my first chapter}
\section{This is my first section, btw it is a very long title}
\chapter{This is the titel of my second chapter}
\end{document}
答案1
如果您想使用语法\section{<title>}{<image>}
,那么您可以重新定义\section
以捕获两个强制参数xparse
:
\documentclass{scrbook}
\usepackage{graphicx}
\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{positioning}
\usetikzlibrary{shadows,calc}
\usepackage[explicit]{titlesec} %Custom Section title
\usepackage{hyperref,xparse}
%%Custom Section-Titles
\newcommand*\sectionlabel{}
\titleformat{\section}
{\gdef\sectionlabel{}
\normalfont\Large\sffamily\bfseries\color{THDblue}}
{\gdef\sectionlabel{\thesection\ }}{0pt}
{%
\begin{tikzpicture}[remember picture]
\node[anchor=north west] (titelbild) at (0,0) {\secimg};
\draw[color=THDblue] ($(titelbild.north east) + (15cm,-0.5cm)$) -- ($(titelbild.north east) + (0.5cm,-0.5cm)$) -- (titelbild.north east) -- (titelbild.north west) -- (titelbild.south west) --(titelbild.south east) --($(titelbild.south east) + (0.5cm,0.5cm)$) -- ($(titelbild.south east) + (15cm,0.5cm)$);
\node[anchor=north west, text width=9cm] at ($(titelbild.north east) + (0.3cm,-0.8cm)$){\sectionlabel#1};
\end{tikzpicture}
}
\let\oldsection\section
\RenewDocumentCommand{\section}{s o m m}{%
\def\secimg{\includegraphics[scale=0.2]{#4}}%
\IfBooleanTF{#1}
{% \section*
\oldsection*{#3}%
}{% \section
\IfValueTF{#2}
{% \section[.]{..}
\oldsection[#2]{#3}
}{% \section{..}
\oldsection{#3}
}%
}%
}
%-------------------------------------
\definecolor{THDblue}{rgb}{0.1000,0.4000,0.4000}
\begin{document}
\chapter{This is the title of my first chapter}
\section{This is my first section, btw it is a very long title}{example-image-a}
\section{This is my second section, btw it is a very long title}{example-image-b}
\section{This is my final section, btw it is a very long title}{example-image-c}
\end{document}
假设每个\section
都会附有一张图片(因此,两个论点\section
被认为是强制的)。