[Tex/LaTex] vertical color gradient over text

colorenvironmentsgradient

I try to get a linear gradient from top to bottom (i.e. black to white) which affects all lines across one or more paragraphs.

The MWE I composed:

\documentclass{article}
\usepackage{lipsum}

\documentclass{article}
\begin{document}
\begin{shadow}
  \lipsum[2]
\end{shadow}
\end{document}

And this is the result I would like to achieve:

rect3787.png

How can I achieve this with an environment or a one-line command?

Best Answer

\documentclass[varwidth, margin=1cm]{standalone} 

\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{lipsum}

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

\noindent \fadingtext{top color=black, bottom color=white, middle color=gray!50}{
    \parbox[b]{\linewidth}{\strut\lipsum[1]}
    }

\end{document}

mwe image

Adapted from this answer to fit the requested black-to-white gradient.


EDIT

By using the basic function style in LaTeX and reasoning outside of the box, you can do the following:

The basic function to give some text to \myfunstuff{Hello world!} will color the text in blue and will restore any following text to black.

\newcommand{\myfunstuff}[1]{\color{blue}{#1}\color{black}}

Which helps you wrap up the previous answer into:

\newcommand{\funstuff}[1]{\fadingtext{top color=black, bottom color=white, middle color=gray!50}{\parbox[b]{\linewidth}{#1}}}

Now onto the small trick you cannot introduce line breaks if you use the parbox command. The solution is to add \vspace*{.5cm} \\ to simulate the effect of paragraphs:

\noindent\funstuff{
LaTeX follows the design philosophy of separating presentation from content, so that authors can focus on the content of what they are writing without attending simultaneously to its visual appearance. In preparing a LaTeX document, the author specifies the logical structure using simple, familiar concepts such as chapter, section, table, figure, etc., and lets the LaTeX system worry about the formatting and layout of these structures. It therefore encourages the separation of layout from content while still allowing manual typesetting adjustments where needed. This concept is similar to the mechanism by which many word processors allow styles to be defined globally for an entire document or the use of Cascading Style Sheets to style HTML. The LaTeX system is a markup language that also handles typesetting and rendering.[7] \vspace*{.5cm} \\ LaTeX can be arbitrarily extended by using the underlying macro language to develop custom formats. Such macros are often collected into packages, which are available to address special formatting issues such as complicated mathematical content or graphics. Indeed, in the example below, the align environment is provided by the amsmath package. \vspace*{.5cm} \\ In order to create a document in LaTeX, you first write a file, say document.tex, using your preferred text editor. Then you give your document.tex file as input to the TeX program (with the LaTeX macros loaded), and TeX writes out a file suitable for viewing onscreen or printing.[8] This write-format-preview cycle is one of the chief ways in which working with LaTeX differs from what-you-see-is-what-you-get word-processing. It is similar to the code-compile-execute cycle familiar to computer programmers. Today, many LaTeX-aware editing programs make this cycle a simple matter of pressing a single key, while showing the output preview on the screen beside the input window. Some online LaTeX editors automatically refresh the preview.[9][10][11][12] Other online tools provide incremental editing in-place, mixed in with the preview in a streamlined single window.[13]
}

If I use lipsum commands LaTeX will complain that there are no lines to break. So I took the following text from wikipedia and voila!

enter image description here

Related Question