2

I would like to include a background graphic on each individual character of a sentence.

enter image description here

Specifically, I would like to have the England flag in each character. Not one flag graphic for the whole sentence like this question, but a flag graphic for each individual character.

From this answer, I've been able to set this MWE up. It currently just shows a red circle on the i and n. I'm struggling to understand how to (a) get an England flag instead of a just a red circle, (b) do this for each character individually.

If it's useful, an England flag graphic was posted in this answer from the question FIFA WorldCup 2018: Flags of Nations. I've included it in my MWE.

Code

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{contour}
\contourlength{0.3pt}
\usepackage{calc}
\renewcommand*\sfdefault{ugq}
\usepackage[T1]{fontenc}


\newlength{\barwidth}
\newcommand{\flagratio}[1]{\gdef\flrat{#1}\setlength{\barwidth}{\flrat pt*100/3}}
\definecolor[named]{EnglishRed}{RGB}{207, 8, 31}
\newcommand{\england}[1][.5]{\flagratio{1.667}\tikz[baseline={(0pt,25pt)},scale=#1]{
\draw[rectangle] (0pt,0pt) -- (0pt,100pt) -- (100pt*\flrat,100pt) -- (100pt*\flrat,0pt) -- cycle;
\draw[line width=20pt*#1,EnglishRed] (0pt,50pt) -- (100pt*\flrat,50pt);
\draw[line width=20pt*#1,EnglishRed] (50pt*\flrat,0pt) -- (50pt*\flrat,100pt);}}

\begin{document}

\england

\begin{tikzfadingfrompicture}[name=A]
\node[scale=20, transparent!0] at (0,0) {\contour{black}{\bfseries\sffamily \textcolor{white}{It's coming home}}};
\end{tikzfadingfrompicture}

\begin{tikzpicture}
\node[scale=20,text=white] at (0,0) {\contour{black}{\bfseries\sffamily \textcolor{white} {It's coming home}}};
\path[path fading=A,fill=red,fit fading=false] (0,0) circle (2);
\end{tikzpicture}

\end{document}
  • So put each character in a different node, inner sep=0pt, anchor=base west. – John Kormylo Jul 6 '18 at 4:03
2

Here is a proposal. First, use a \savebox for the flag since otherwise one would have to nest tikzpictures. For the same reason, I use Ulrike Fischer's trick to process the characters. In principle, his is also possible with text effects that come with the decorations.text library, but my attempts always had some nesting of tikzpictures involved.

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{contour}
\contourlength{0.3pt}
\usepackage{calc}
\renewcommand*\sfdefault{ugq}
\usepackage[T1]{fontenc}
\usepackage{soul}


\newsavebox\England

\newlength{\barwidth}
\newcommand{\flagratio}[1]{\gdef\flrat{#1}\setlength{\barwidth}{\flrat pt*100/3}}
\definecolor[named]{EnglishRed}{RGB}{207, 8, 31}
\newcommand{\england}[1][.5]{\flagratio{1.667}\tikz[baseline={(0pt,25pt)},scale=#1]{
\draw[rectangle] (0pt,0pt) -- (0pt,100pt) -- (100pt*\flrat,100pt) -- (100pt*\flrat,0pt) -- cycle;
\draw[line width=20pt*#1,EnglishRed] (0pt,50pt) -- (100pt*\flrat,50pt);
\draw[line width=20pt*#1,EnglishRed] (50pt*\flrat,0pt) -- (50pt*\flrat,100pt);}}
\sbox\England{\england[1]}

\newcommand{\PlaceCharOverEnglandFlag}[2][20]{%
\begin{tikzfadingfrompicture}[name=temp]
\node[transparent!0,scale=#1] 
{\contour{black}{\bfseries\sffamily \textcolor{white}{#2}}};
\end{tikzfadingfrompicture}%
\tikz[baseline=(X.base)]{\node[inner sep=0pt,outer sep=0pt,scale=#1] (X) {\contour{black}{\bfseries\sffamily 
\textcolor{white}{#2}}};
\path[path fading=temp,fit fading=false,overlay] node {\usebox\England};}%
}

\makeatletter % from https://tex.stackexchange.com/a/2709/121799
\def\SOUL@soeverytoken{%
 \PlaceCharOverEnglandFlag{\the\SOUL@token}}
\makeatother

\begin{document}
\so{It's}\hspace*{2cm}\so{coming}\hspace*{2cm}\so{home!}
\end{document}

enter image description here

EDIT: Just for fun, a version using the above-mentioned text effects. In a first step, for every character a savebox is created, which is then called with the character command trick. Unfortunately, that does not work with special characters like space.

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings,decorations.text}
\usepackage{contour}
\contourlength{0.3pt}
\usepackage{calc}
\renewcommand*\sfdefault{ugq}
\usepackage[T1]{fontenc}

\definecolor{SkyBlue}{rgb}{0.00784314,0.32156864,0.61176473}
\definecolor{FireRed}{rgb}{0.86274511,0.11764706,0.20784314}
\newsavebox\Iceland
\sbox\Iceland{\begin{tikzpicture}[scale=0.5]
\fill[SkyBlue] (0,0) rectangle (25,18);
\fill[white] (7,0) rectangle (11,18);
\fill[white] (0,7) rectangle (25,11);
\fill[FireRed] (8,0) rectangle (10,18);
\fill[FireRed] (0,8) rectangle (25,10);
\end{tikzpicture}
}

\newcommand{\PlaceCharOverIcelandFlag}[2][20]{%
\begin{tikzfadingfrompicture}[name=temp]
\node[transparent!0,scale=#1] 
{\contour{black}{\bfseries\sffamily \textcolor{white}{#2}}};
\end{tikzfadingfrompicture}%
\tikz[baseline=(X.base)]{\node[inner sep=0pt,outer sep=0pt,scale=#1] (X) {\contour{black}{\bfseries\sffamily 
\textcolor{white}{#2}}};
\path[path fading=temp,fit fading=false,overlay] node {\usebox\Iceland};}%
}

% from https://tex.stackexchange.com/a/268979/121799
\newenvironment{glrbox}[1]
 {\def\thisbox{#1}\begin{lrbox}{0}}
 {\end{lrbox}\global\setbox\thisbox=\box0\relax}

\foreach \X in {a,...,z}
{
   \expandafter\newsavebox\csname LetterBox\X\endcsname
   \begin{glrbox}{\csname LetterBox\X\endcsname}
    \PlaceCharOverIcelandFlag{\X}
   \end{glrbox}
}
\foreach \X in {A,...,Z}
{
   \expandafter\newsavebox\csname LetterBox\X\endcsname
   \begin{glrbox}{\csname LetterBox\X\endcsname}
    \PlaceCharOverIcelandFlag{\X}
   \end{glrbox}
}

\begin{document}
% ugly, need to use a character, here X, as a space replacement
\sbox\LetterBoxX{\tikz[baseline=(X.base),scale=20]{\node[inner
sep=0pt,text=white,scale=10] (X) {X};}}
\newcommand{\mycommand}[1]{\expandafter\usebox\csname LetterBox#1\endcsname}
\begin{tikzpicture}[decoration={text effects along path, 
text={MarmotsXsupportXIceland},
text effects/.cd,
path from text,
every word separator/.style={fill=red!30}, 
characters={text along path, character command=\mycommand}}]
\path [decorate] (0,0); 
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.