[Tex/LaTex] Draw Text in different shapes

text;tikz-pgf

I'm trying to draw text in shapes (using scaling and path). I want text to show up like this:

Valley Text

I've tried a lot of different methods, but I'm having trouble doing so. I can get close to it using something like this:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.text}

\begin{document}
\pagenumbering{gobble}

\begin{tikzpicture}
\node (One) at (-15,0) {.}; 
\node (Two) at (15,0) {.};
\draw [decorate,decoration={text effects along path, 
                    text={TTTTTTTTTTTTTTTT},
                    text align=center,
                    text effects/.cd,
                    path from text,
                    character count=\i, character total=\n, 
                    characters= {text along path, scale=(2 + abs(\i - (\n / 2) - 0.5) * 1 )}}] (One) to (Two);

\end{tikzpicture}

\end{document}

Which results in :

Result

I want the top of the text to curve up like the example shown.

Best Answer

I just threw in the \outline macro (requires pdflatex) for the fun of it.

The \outline stuff is in the preamble, whereas the necking code is in the main document. I originally stole the code that makes up \outline from Malipivo at TikZ: halo around text?

What I do is create a box (\mytext) with the text (padded a little so that none gets subsequently clipped), and then successively apply a \clipbox that vertically slices the text box \cuts times. On each cut, I apply a \scalebox to shrink the text as a function of the cut number and \dip, which represents the maximum necking fraction. The function I choose here is parabolic, though others can be developed as well.

\documentclass{article}
\usepackage{ifthen,trimclip,calc,fp,graphicx,xcolor}
%%%%% FOR TEXT OUTLINING
\input pdf-trans
\newbox\qbox
\def\usecolor#1{\csname\string\color@#1\endcsname\space}
\newcommand\bordercolor[1]{\colsplit{1}{#1}}
\newcommand\fillcolor[1]{\colsplit{0}{#1}}
\newcommand\outline[1]{\leavevmode%
  \def\maltext{#1}%
  \setbox\qbox=\hbox{\maltext}%
  \boxgs{Q q 2 Tr \thickness\space w \fillcol\space \bordercol\space}{}%
  \copy\qbox%
}
\newcommand\colsplit[2]{\colorlet{tmpcolor}{#2}\edef\tmp{\usecolor{tmpcolor}}%
  \def\tmpB{}\expandafter\colsplithelp\tmp\relax%
  \ifnum0=#1\relax\edef\fillcol{\tmpB}\else\edef\bordercol{\tmpC}\fi}
\def\colsplithelp#1#2 #3\relax{%
  \edef\tmpB{\tmpB#1#2 }%
  \ifnum `#1>`9\relax\def\tmpC{#3}\else\colsplithelp#3\relax\fi
}
\bordercolor{blue}
\fillcolor{yellow}
\def\thickness{.5}
%%%%%
\begin{document}
\Huge
\edef\dip{.5}% percent to depress the amplitude
\def\cuts{200}% Number of cuts
\newsavebox\mytext
\savebox{\mytext}{\kern.3pt\textbf{\textsf{\outline{Valley Text}}}\kern.3pt}% TEXT
\newlength\clipsize
\FPeval{\myprod}{1/cuts}
\clipsize=\myprod\wd\mytext\relax
\newcounter{mycount}
\whiledo{\value{mycount}<\cuts}{%
  \stepcounter{mycount}%
  \edef\NA{\themycount}%
  \edef\NB{\the\numexpr\cuts-\themycount\relax}%
  \FPeval{\myprod}{1 - \dip*\NA*\NB*4/\cuts/\cuts}%
  \clipbox{%
    \value{mycount}\clipsize\relax{} %
    -1pt %
    \wd\mytext-\value{mycount}\clipsize-\clipsize\relax{} %
    -1pt%
  }{\scalebox{1}[\myprod]{\usebox{\mytext}}}%
}
\end{document}

enter image description here

Without the \outline code, the code is more manageable. (EDITED) Here I convert it into a macro \parabtext[<mode>]{<lift>}{<neck>}{<cuts>}{<content>} with several additions from above:

<mode> is 0 for narrow middle, and 1 for narrow ends;

<lift> is the fractional max-lift of the baseline during the transformation

<neck> is the fractional reduction in total height at the neck;

<cuts> are the number of vertical slices to apply to the box (too small, and it will look stair-stepped)

<content> is the stuff to put in a box and transform.

\documentclass{article}
\usepackage{ifthen,trimclip,calc,fp,graphicx,xcolor}
\newsavebox\mytext
\newcounter{mycount}
\newlength\clipsize
\newcommand\parabtext[5][0]{%
  \edef\neck{#3}% percent to depress the amplitude
  \def\cuts{#4}% Number of cuts
  \savebox{\mytext}{\kern.2pt#5\kern.2pt}% TEXT
  \FPeval{\myprod}{1/cuts}%
  \clipsize=\myprod\wd\mytext\relax%
  \setcounter{mycount}{0}%
  \whiledo{\value{mycount}<\cuts}{%
    \stepcounter{mycount}%
    \edef\NA{\themycount}%
    \edef\NB{\the\numexpr\cuts-\themycount\relax}%
    \FPeval{\myprod}{\NA*\NB*4/\cuts/\cuts}%
    \ifnum0#1=0\relax%
      \FPeval{\myprod}{1 - \neck*(\myprod)}%
    \else%
      \FPeval{\myprod}{1 - \neck*(1-\myprod)}%
    \fi%
    \clipbox{%
      \value{mycount}\clipsize\relax{} %
      -1pt %
      \wd\mytext-\value{mycount}\clipsize-\clipsize\relax{} %
      -1pt%
    }{\raisebox{#2\dimexpr\ht\mytext-\myprod\ht\mytext}{%
        \scalebox{1}[\myprod]{\usebox{\mytext}}}}%
  }%
}
\begin{document}
\Huge\centering\def\X{\textbf{XXX}}%
\parabtext{0}{.7}{200}{\textbf{\textcolor{brown}{Valley Text}}}\par
\X\parabtext{0}{.7}{200}{\X}\parabtext[1]{0}{.7}{200}{\X}\X\par
\X\parabtext{1}{.7}{200}{\X}\parabtext[1]{1}{.7}{200}{\X}\X\par
\X\parabtext{.425}{.7}{200}{\X}\parabtext[1]{.425}{.7}{200}{\X}\X
\end{document}

enter image description here

The most recently EDITED option, <mode>, changes the shape function of the transformation. In both modes, a function is evaluated

\FPeval{\myprod}{\NA*\NB*4/\cuts/\cuts}%

That is a parabola that is at a value of 1 at both ends of the box and a value of 0 at the middle of the box. Then, if the mode is 0, the transformation is

\FPeval{\myprod}{1 - \neck*(\myprod)}%

whereas, if not zero, the transformation is

\FPeval{\myprod}{1 - \neck*(1-\myprod)}%

This simple difference will produce the necked and barrelled versions, respectively.


PROPOSED METHODOLOGY

The method is somewhat computationally expensive, but it is intended to be used sparingly for the Wow! effect.

First, I'll show a slightly modified code that takes it, in effect, from a forward difference to a central difference, though it may cost a little more in computation.

But what I think works best is to set the number of slices in a user defined variable to a low number, say \def\slices{5} and develop your document on that basis. On the final compilation, when all is set in the final layout, then re\def\slices to the desired number, say {200}, and recompile one last time.

\documentclass{article}
\usepackage{ifthen,trimclip,calc,fp,graphicx,xcolor}
\newsavebox\mytext
\newcounter{mycount}
\newlength\clipsize
\newcommand\parabtext[5][0]{%
  \edef\neck{#3}% percent to depress the amplitude
  \def\cuts{#4}% Number of cuts
  \savebox{\mytext}{\kern.2pt#5\kern.2pt}% TEXT
  \FPeval{\myprod}{1/cuts}%
  \clipsize=\myprod\wd\mytext\relax%
  \setcounter{mycount}{0}%
  \whiledo{\value{mycount}<\cuts}{%
    \stepcounter{mycount}%
    \edef\NA{\themycount}%
    \edef\NB{\the\numexpr\cuts-\themycount\relax}%
    \FPeval{\myprod}{(\NA-.5)*(\NB+.5)*4/\cuts/\cuts}%
    \ifnum0#1=0\relax%
      \FPeval{\myprod}{1 - \neck*(\myprod)}%
    \else%
      \FPeval{\myprod}{1 - \neck*(1-\myprod)}%
    \fi%
    \clipbox{%
      \value{mycount}\clipsize-\clipsize\relax{} %
      -1pt %
      \wd\mytext-\value{mycount}\clipsize\relax{} %
      -1pt%
    }{\raisebox{#2\dimexpr\ht\mytext-\myprod\ht\mytext}{%
        \scalebox{1}[\myprod]{\usebox{\mytext}}}}%
  }%
}
\begin{document}
\def\slices{200}
\Huge\centering\def\X{\textbf{XXX}}%
\parabtext{0}{.7}{\slices}{\textbf{\textcolor{brown}{Valley Text}}}\par
\X\parabtext{0}{.7}{\slices}{\X}\parabtext[1]{0}{.7}{\slices}{\X}\X\par
\X\parabtext{1}{.7}{\slices}{\X}\parabtext[1]{1}{.7}{\slices}{\X}\X\par
\X\parabtext{.425}{.7}{\slices}{\X}\parabtext[1]{.425}{.7}{\slices}{\X}\X
\end{document}

Here is, for example, the output with \slices set to 5 for the lo-res compilation:

enter image description here

NOTE TO XeLaTeX USERS

The OP noted that this approach failed to work in XeLaTeX. After some study, I boiled the problem down to a bug in the trimclip package (\clipbox of a \scalebox not working properly in xelatex). Joseph Wright was kind enough to diagnose the issue and provide a patch for the trimclip code, when running in XeLaTeX.

\makeatletter
\ifdefined\XeTeXversion
  \def\@cliptoboxdim#1{%
    \setbox #1=\hbox{%
      \Gin@defaultbp\WIDTH{\wd #1}%
      \Gin@defaultbp \DEPTH {\dp #1}%
      \@tempdima \ht #1%
      \advance\@tempdima\dp#1%
      \Gin@defaultbp \TOTALHEIGHT {\@tempdima }%
      \special{pdf:literal q}% 
      \special{pdf:literal 0 -\DEPTH \space \WIDTH \space \TOTALHEIGHT \space re W n }%
      \rlap{\copy #1}%
      \special {pdf:literal Q}%
      \hskip\wd#1%
    }%
  }
\fi
\makeatother