1

I would like to add gradient color for lines of words and long ones are just causing trouble.

Here is the code

\documentclass{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}

\usetikzlibrary{fadings}
\newcommand\fadingtext[3][]{%
  \begin{tikzfadingfrompicture}[name=fading letter]
    \node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
  \end{tikzfadingfrompicture}%
  \begin{tikzpicture}[baseline=(textnode.base)]
    \node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
    \shade[path fading=fading letter,#2,fit fading=false]
    (textnode.south west) rectangle (textnode.north east);%
  \end{tikzpicture}%
}

\begin{document}

\pagestyle{plain}

\noindent
\fadingtext{left color=YellowOrange, right color = Cyan}{In physics, relativistic angular momentum refers to the mathematical formalisms and physical concepts that define angular momentum in special relativity (SR) and general relativity (GR). The relativistic quantity is subtly different from the three-dimensional quantity in classical mechanics.}

\

\noindent
\fadingtext{left color=YellowOrange, right color = Cyan}{In physics, relativistic angular momentum refers to the mathematical formalisms and} \fadingtext{left color=Green, right color = RawSienna}{physical concepts that define}

\

\noindent
In physics, relativistic angular momentum refers to the mathematical formalisms and physical concepts that define angular momentum in special relativity (SR) and general relativity (GR). The relativistic quantity is subtly different from the three-dimensional quantity in classical mechanics.

\end{document}

And apparently the line breaking is not working here

Line Break Not Working

We can see that the colored text is going beyond the range of the margin.

Thanks in advance for anyone who helps.


While using

\parbox{\textwidth}

in the text really helps. But for text with different color gradient in same line, there would be trouble

\noindent
\fadingtext{left color=YellowOrange, right color = Cyan}{\parbox{\textwidth}{In physics,}} \fadingtext{left color=Black, right color = Red}{\parbox{\textwidth}{relativistic angular momentum refers}}

The output looks like

Different Color Gradient in Same Line

  • \phantom{#3} reserves space for one long line of text, even if you include [text width=\textwidth]. You can however use a \parbox{\textwidth}{...} for #3. – John Kormylo May 6 '16 at 1:28
  • I tried and the text start overlapping each other. I have updated the picture in the question. – zyy May 7 '16 at 20:09
3

What I meant was to put a \parbox into #3:

\documentclass{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}

\usetikzlibrary{fadings}
\newcommand\fadingtext[3][]{%
  \begin{tikzfadingfrompicture}[name=fading letter]
    \node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
  \end{tikzfadingfrompicture}%
  \begin{tikzpicture}[baseline=(textnode.base)]
    \node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
    \shade[path fading=fading letter,#2,fit fading=false]
    (textnode.south west) rectangle (textnode.north east);%
  \end{tikzpicture}%
}

\begin{document}

\pagestyle{plain}

\noindent
\fadingtext{left color=YellowOrange, right color = Cyan}{\parbox{\textwidth}{%
In physics, relativistic angular momentum refers to the mathematical formalisms and physical concepts that define angular momentum in special relativity (SR) and general relativity (GR). The relativistic quantity is subtly different from the three-dimensional quantity in classical mechanics.}}

\

\noindent
\fadingtext{left color=YellowOrange, right color = Cyan}{In physics, relativistic angular momentum refers to the mathematical formalisms and} \fadingtext{left color=Green, right color = RawSienna}{physical concepts that define}

\

\noindent
In physics, relativistic angular momentum refers to the mathematical formalisms and physical concepts that define angular momentum in special relativity (SR) and general relativity (GR). The relativistic quantity is subtly different from the three-dimensional quantity in classical mechanics.

\end{document}

demo

  • Yeah, it really works and thank you very much. But if I would like to use different color gradient for text in same line \noindent \fadingtext{left color=YellowOrange, right color = Cyan}{\parbox{\textwidth}{In physics,}} \fadingtext{left color=Black, right color = Red}{\parbox{\textwidth}{relativistic angular momentum refers}} would cause trouble and I have updated in the question. – zyy May 10 '16 at 0:42
  • 1
    First, you can break text into lines using a \savebox and \vsplit. Then you can apply a different fading to each box and stack the resulting nodes vertically. See tex.stackexchange.com/questions/295039/… on the use of \vsplit. – John Kormylo May 10 '16 at 2:03
  • I added \noindent to keep the text aligned and thank you so much for helping out! – zyy May 14 '16 at 17:20
1

This is not a tikz solution, but a different approach. It is based on my answers at Make fireworks with only text and How can I 'fade out' text after a certain length?. It can successfully wrap lines.

EDITED for sloppypar and now uses \colorlet instead of \def, which allows mixed colors to be specified as \FadeColors arguments. After specifying colors as \FadeColors{<start-color>}{<end-color>}, one uses the syntax \FadeAfter{<length>}{content}. The fadeout is done as a function of the current text length, as opposed to being applied en masse to a rectangular text box.

\documentclass{article}
\usepackage{xcolor,ifthen}
\newcounter{tmpcounter}
\newlength\cumlength
\newlength\critlength
\newlength\tmplength
\newcount\mynum
\newcount\myden
\makeatletter
\newcommand\FadeColors[2]{%
    \colorlet{innercolor}{#1}%
    \colorlet{outercolor}{#2}%
}

\newcommand\FadeAfter[2]{%
  \critlength=#1\relax\cumlength=0pt\relax%
  \def\cumstring{}\fahelp{#2}}
\newcommand\fahelp[1]{\prefahelper#1 \relax\fahelper#1\relax}
\def\prefahelper#1#2 #3\relax{\gdef\wordremaining{#1#2}}
\def\fahelper#1#2\relax{%
  \global\protected@edef\cumstring{\cumstring#1}%
  \ifthenelse{\equal{#1}{\wordremaining}}{%
    \global\protected@edef\cumstring{\cumstring\ }}{}%
  \setbox0=\hbox{\cumstring}%
  \tmplength=.01\critlength\relax%
  \mynum=\wd0\relax%
  \myden=\tmplength\relax%
  \divide\mynum by\myden%
  \setcounter{tmpcounter}{\numexpr100-\the\mynum}%
  \ifnum\thetmpcounter<0\setcounter{tmpcounter}{0}\fi%
  \textcolor{innercolor!\thetmpcounter!outercolor}{#1}%
  \ifthenelse{\equal{#1}{\wordremaining}}{\ }{}%
    \ifx\relax#2\relax\else\fahelp{#2}\fi%
}
\makeatother
\begin{document}
\FadeColors{black}{white}
\FadeAfter{\dimexpr\textwidth+1in}{This is a very long multi-line test.
This is a very long test. This is a very long test.
This is a very long test. }\par
\FadeColors{red}{blue!70!cyan!50}
\FadeAfter{2in}{This is a test. This is a test.
This is a test.}\par
\FadeColors{yellow!50!orange}{cyan}
\begin{sloppypar}
\FadeAfter{4\textwidth}{In physics, relativistic angular momentum refers to 
the mathematical formalisms and physical concepts that define angular 
momentum in special relativity (SR) and general relativity (GR). The 
relativistic quantity is subtly different from the three-dimensional 
quantity in classical mechanics.}
\end{sloppypar}
\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.