# Formatting figure captions overlayed with TikZ

I am using full-page figures as cover images for chapters, see Marginless full page figure with caption overlay magazine-like.

However, I am unsatisfied with the format of the caption. The following code produces a white block for the caption, which for some reasons starts with a blank line. I would like to

• have removed this blank line,
• use transparency for the background rectangle,
• be able to use white text for the caption and caption label (as a local option, not a global setting),
• alternatively, have a gray rectangle going down to the page border, with a newline after the caption label.

Here is the code:

\documentclass[a4paper,11pt]{book}

\usepackage{tikz}
\usepackage{lipsum}
\usepackage{eso-pic}

%coordinate system for page runs -1 to +1
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
\parsecomma#1\endparsecomma
\pgfpointanchor{current page}{north east}
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfpointanchor{current page}{south west}
\pgf@xb=\pgf@x%
\pgf@yb=\pgf@y%
\pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
\expandafter\pgf@x\expandafter=\pgfmathresult pt
\pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
\expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother

% image
\usepackage{capt-of}
\newcommand\chaptercover[3]{%
\clearpage
\thispagestyle{empty}
% caption
\begin{tikzpicture}[remember picture,overlay]%
\node [fill=white, anchor = north west, inner xsep = 3mm,inner ysep = 3mm, text width=6cm] at (page cs:-0.9,-0.75) {\captionof{figure}[#2]{#2}\label{#3}};
\end{tikzpicture}
\clearpage
}

\begin{document}
test
\chaptercover{test.jpg}{Caption for a space image in space.}{fig:test}
\end{document}

• Can you link the cover image? Dec 25 '15 at 20:10
• You put caption in the box, for which you require fill=white. Dec 25 '15 at 20:26
• Are you using caption just to advance the figure counter? Dec 25 '15 at 20:26
• Yes, I need to reference this figure in the text later on, this is why I need a figure label and to advance the counter. Here is the example picture: freeimageslive.com/galleries/space/nebula/pics/… Dec 25 '15 at 21:02
• \captionof or \caption insert a new \par and extra vertical space \abovecaptionskip to fix this problem you can try \setlength\abovecaptionskip{-10pt}\captionof{figure}[#2]{#2}\label{#3} Dec 25 '15 at 21:37

I think you're using the capt-of package just to have the Figure # counter advance for each chapter. In that case, you don't need to load this package at all. I have set a new counter, called myfig, then issued a \newcommand that is your custom label, that basically uses the counter as if it was a normal reference using a figure. Each time the custom label is issued (same as your command), the counter advances by 1.

Then your node text will simply be Figure~\arabic{myfig}: #2. This way, every time you'll issue the \chaptercover command, the counter —and therefore the reference in turn— will show the appropriate number and you won't have to deal with the blank line.

For fill opacity, write opacity=.5 where 0 is completely transparent and 1 is completely opaque. This will make the text transparent too, but you just need to add text opacity=1 to fix this. Also, write text=white to have white text.

Here's the code that shows all these changes:

\documentclass[a4paper,11pt]{book}

\usepackage{tikz}
\usepackage{lipsum}
\usepackage{eso-pic}

%coordinate system for page runs -1 to +1
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
\parsecomma#1\endparsecomma
\pgfpointanchor{current page}{north east}
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfpointanchor{current page}{south west}
\pgf@xb=\pgf@x%
\pgf@yb=\pgf@y%
\pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
\expandafter\pgf@x\expandafter=\pgfmathresult pt
\pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
\expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother

\newcounter{myfig}
\newcommand{\figref}[1]{\refstepcounter{myfig}\label{#1}}

% image
\newcommand\chaptercover[3]{%
\clearpage
\thispagestyle{empty} %
% caption
\figref{#3}
\begin{tikzpicture}[remember picture,overlay]%
\node [fill=white, anchor = north west, text=white, opacity=.5, text opacity=1, inner xsep = 3mm,inner ysep = 3mm, text width=6cm] at (page cs:-0.9,-0.75) {Figure~\arabic{myfig}: #2};
\end{tikzpicture}
\clearpage
}

\begin{document}
%test
\chaptercover{example-image-a}{Caption for a space image in space.}{test}

\lipsum[1]

See fig.~\ref{test}

\chaptercover{example-image-b}{Yet another caption for a space image in space.}{test2}

And now see fig.~\ref{test2}
\end{document}

• Thanks a lot for the comprehensive answer! However, your alternative caption does not seem to be robust. In the chaptercover, I now add 1 to the chapter counter before generating the caption, and -1 afterwards. So the figure refers to the following chapter number. But in the text later on the referenced figure number is 1 less than the caption figure number. This was not an issue using captionof. Dec 25 '15 at 21:45
• @Martin So basically you'd like to be able to say "See \ref{test}" (for example) to refer to the first cover? Dec 25 '15 at 21:52
• Correct, this is the intention, otherwise I would generate the page elsewhere and simply include it with pdfpages. I decribed my needs in more detail in the linked question. Dec 25 '15 at 22:02
• @Martin Fixed the reference issue. Feel free to test, let me know if it works. Dec 25 '15 at 22:57
• Thanks, that's it. Bright idea to invoke a new counter! Could you please remove your last two comments in order to tidy up this discussion a bit? Dec 25 '15 at 23:30

Try if the following redesign of caption "box" is what you looking for:

\begin{tikzpicture}[remember picture,overlay]%
\node [anchor = north west, inner sep = 3mm, text width = 6cm]
at (page cs:-0.9,-0.75)
{\captionsetup[figure]{labelfont={bf,color=white},textfont={color=white}}
\captionof{figure}[#2]{#2}\label{#3}};
\end{tikzpicture}


Edit: It seems that caption set up overwrite the caption label and text color. This can be avoided by local set up, where you define color just for this caption. The above MWE is corrected accordingly. I assume, that document in the preamble load packages color or xcolor and caption.

• text=white has no effect on my caption color. Maybe it is overwritten by \captionof? Dec 25 '15 at 21:27
• @Martin, seems to be this a case. See edit of my answer, maybe this help. Dec 25 '15 at 21:34
• Thanks for your effort. I tried this already, but it only changes the color of caption text, not the caption label. I could globally change caption label color in the preamble, however I need black labels elsewhere and I do not know how to set this locally only for these kind of figures in the document. Dec 25 '15 at 21:52
• @Martin, I figured out, how rto managed this. See recent edit of my answer. Dec 25 '15 at 21:54

I want to add a solution for the third, alternative figure style with the gray box (sub-figure on the right in the question). It is based on Alenanno's answer, I only show the important changes here:

\usepackage{xcolor}
\definecolor{gray85}{gray}{.85}
...
\begin{tikzpicture}[remember picture,overlay]%
\node[text width=5cm, minimum height= 4cm, fill=gray85,
anchor = north west] (node1) at (-2cm,-22cm) {};
\node[text width=5cm, text=black,
inner xsep = 3mm, inner ysep = 3mm,
anchor = north west] at (node1.north west) {%
\textbf{Fig.~\arabic{myfig}:}\\ #2};
\end{tikzpicture}


It utilizes two nodes within each other, to vertically align the text at the top of the box.