5

I'm trying to translate a wordpress-blog to a book. Since every article has a title and a titleimage I am looking for a way to pass a titelimage to a section.

I figured out how to create a nice looking section using the tikz package but now I'm wondering how to pass the corresponding picture automatically and not by hand.

Something like \section{title}{picture.jpg} would be great. Any suggestions on how to do this?

Here is what i got so far:

\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}
7
  • 2
    May I suggest using something like \includegraphics[width=2cm]{example-image} for your example? We don't have the image you refer to in the code of course, but almost everyone will have example-image, as it's part of a package called mwe that is included in most LaTeX distributions. Nov 20, 2016 at 15:31
  • 2
    You could simply define a new command \newcommand*\mysection[1]{\def\secimage{#1}\section}. This way, you just say \mysection{<image>}{<title>} and you may refer to your image as \secimage. Nov 20, 2016 at 16:02
  • I edited the code to work with example-image. Didn't know about this possibility. I'm pretty new in TeX ;-) @EricDomenjoud: Now I need to define \secimage somewhere aswell, right? I need to do the link between \secimage and the includegraphics where i can set the width of the image.
    – Flo
    Nov 20, 2016 at 16:45
  • I don't really get it. Can I really use this new \secimage command inside the \titleformat{\section} block? If so, how? I need to have access to the picture, while the sectiontitle is being written by tikz. I'm sorry, I bet this is all basic stuff for you guys but I'm really having a hard time.
    – Flo
    Nov 20, 2016 at 17:05
  • 2
    @Flo. Yes, after \mysection{<image>}{<title>}, \secimage is defined as the name of your image and you may say \includegraphics[scale=0.2]{\secimage} in the \titleformat{\section} block. The command \mysection{<image>} actually first defines \secimage and then calls the normal \section command. This means that you can also use \mysection{<image>}[<short title>]{<title>} or \mysection{<image>}*{<title>}. Nov 20, 2016 at 18:24

1 Answer 1

2

If you want to use the syntax \section{<title>}{<image>}, then you can redefine \section to capture two mandatory arguments using xparse:

enter image description here

\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}

It is assumed the each \section will be accompanied by a picture (hence the fact that the two arguments for \section is considered mandatory).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .