使用 TikZ 在单词的每个字符上添加背景图像

使用 TikZ 在单词的每个字符上添加背景图像

我想在句子的每个字符上添加一个背景图形。

在此处输入图片描述

具体来说,我希望每个字符都包含英格兰国旗。而不是像这个问题,而是每个角色都有一面旗帜图形。

这个答案,我已经能够设置这个 MWE。目前它只在和上显示一个红色圆圈in我很难理解如何 (a) 获取英格兰国旗而不是一个红色圆圈,(b) 为每个角色单独执行此操作。

如果有用的话,英格兰国旗图片已发布在这个答案从问题2018 年国际足联世界杯:各国国旗。我已将其包含在我的 MWE 中。

代码

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

答案1

这里有一个建议。首先,使用 a\savebox作为标志,否则必须嵌套tikzpictures。出于同样的原因,我使用乌尔里克·菲舍尔的绝招处理字符。原则上,这也是使用库text effects自带的函数可以实现的decorations.text,但我的尝试总是tikzpictures涉及到一些嵌套。

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

在此处输入图片描述

编辑:只是为了好玩,使用上面提到的版本text effects。第一步,为每个字符savebox创建一个,然后用character command技巧调用它。不幸的是,这不适用于空格等特殊字符。

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

在此处输入图片描述

相关内容