# Background clip to text Tikz

I'm using Tikz to design the cover of a book and I need to do something like this :

I've tried to use some tricks given here but it did nothing.

I'm actually using the following code:

\makeatletter
\renewcommand{\maketitle}{%
\begin{titlepage}%
\pagestyle{empty}%
\begin{tikzpicture}[overlay,remember picture]%

% Logo
\node[opacity=1,inner sep=0pt] at ([yshift=-0.09\paperheight]current page.north){\includegraphics[width=0.35\paperwidth]{logo.eps}};

% Document title
\node (booktitle) at ([yshift=-.45\paperheight]current page.north) [above, text width=.8\paperwidth, font=\fontsize{40pt}{43pt}\selectfont, color=covertext, align=center] {\bfseries\@title};%

\end{tikzpicture}%
\end{titlepage}%
\setcounter{footnote}{0}%
}
\makeatother


• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem and shows the effort you have made. It will be much easier for us to help you when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Astrinus Jun 11 '15 at 8:51
• You can also check this related question and this one – Kpym Jun 13 '15 at 19:47

## Solution without TikZ

Using text as clip path is supported by package pdfrender if pdfTeX (or LuaTeX) is running in PDF mode.

\pdfsave and \pdfrestore saves and restores the current graphics state, thus that the clipping ends after \pdfrestore. Since the graphics state include the current transfer matrix (e.g., the current point on the PDF page), the command pair must be used on the same TeX location. Otherwise the TeX coordinate system and the PDF coordinate system would go out of sync.

Full example:

\documentclass{article}
\usepackage{graphicx}
\usepackage{pdfrender}
\usepackage{tgheros}
\usepackage{varwidth}

\newcommand*{\TextImage}[2]{%
\sbox0{%
\sffamily
\bfseries
\begin{varwidth}{\linewidth}%
\uppercase{\ignorespaces#1\ifhmode\unskip\fi}%
\end{varwidth}%
}%
\mbox{%
\pdfsave
% Set clip path
\pdfrender{TextRenderingMode=Clip}%
\rlap{%
\copy0 %
}%
% Now the image or whatever is provided in #2 is used.
\rlap{%
\def\width{\wd0 }%
\def\depth{\dp0 }%
\def\height{\ht0 }%
\def\totalheight{\dimexpr\ht0+\dp0\relax}%
\raisebox{-\dp0}{#2}%
}%
\pdfrestore
% Inform TeX about the text dimensions
\phantom{\copy0}%
}%
}

\begin{document}
\TextImage{%
This text clips the\\
background image%
}{%
\includegraphics[
width=\width,
height=\totalheight,
viewport=0 0 {10mm} {5mm},
]{bg.pdf}%
}
\end{document}


The background image bg.pdf was generated with:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfmathsetseed{10000}
\foreach \i in {1, ..., 500} {
\pgfmathsetmacro\colred{rnd}
\pgfmathsetmacro\colgreen{rnd}
\pgfmathsetmacro\colblue{rnd}
\definecolor{col}{rgb}{\colred,\colgreen,\colblue}
}
\pgfresetboundingbox
\useasboundingbox (0, 0) (1, 1);
\end{tikzpicture}
\end{document}


This is a slightly tweaked solution based on Mark Wibrow's answer.

It adds the fit library so that the front node (the one with the text) stretches to cover the background image without having to set the size manually.

This is the image source (place it in your Tex folder where the main document is, and rename it vegetation).

## Code

\documentclass[12pt,a4paper]{article}
\usepackage[margin=.5in]{geometry}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage{graphicx}

\makeatletter
\tikzset{
outline text/.style={
execute at begin node={%
\pgfsetfillopacity{0}%
\pgfsetlinewidth{\pgflinewidth}%
\pgfsetstrokecolor{#1}%
\special{pdf:literal 1 Tr }%
},
},
% Interrupt the picture to create a fading.
\pgfinterruptpicture%
\let\tikz@atbegin@node=\relax%
\node [node contents=, #1,text=transparent!100, fill=transparent!0];%
{\noexpand\pgfpoint{\the\pgf@picmaxx}{\the\pgf@picmaxy}}}%
\endpgfinterruptpicture%
}%
\tikzset{#1}%
}
}
\makeatother

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[line join=round]

\node (image) at (0,0) {\includegraphics[scale=.6]{vegetation}};
\node[font=\sffamily\bfseries\fontsize{50}{40}\selectfont,text width=18cm, align=center,outline text=white, fit=(image.south west)(image.north east),       knockout text fading={
fill=white, draw=white,
node contents={THIS TEXT CLIPS THE BACKGROUND IMAGE}}];

\end{tikzpicture}
\end{figure}
\end{document}


In TikZ you can use tikzfadingfrompicture from fadings library to clip using text.

In the following example I use a shaded rectangle in place of your image.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

% the clipping text
\node [text=transparent!0, text width=0.7\paperwidth, align=center, font=\fontfamily{ptm}\bfseries\scshape, scale=3]
{This text clips the\\ background immage};

% use it to clip your image
\begin{tikzpicture}