# How to implement automatic line breaking in gradient colored text?

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}

\node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
\begin{tikzpicture}[baseline=(textnode.base)]
\node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
(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

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

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

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

\documentclass{article}

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

\node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
\begin{tikzpicture}[baseline=(textnode.base)]
\node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
(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}


• 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
• 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

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
\colorlet{innercolor}{#1}%
\colorlet{outercolor}{#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}
\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