文本上的垂直颜色渐变

文本上的垂直颜色渐变

我尝试获得从上到下(即从黑到白)的线性渐变,这会影响一个或多个段落的所有行。

我编写的 MWE:

\documentclass{article}
\usepackage{lipsum}

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

这是我想要实现的结果:

直角三角形

如何通过环境或单行命令实现这一点?

答案1

\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 图像

改编自此回答以适合所要求的黑白渐变。


编辑

通过使用 LaTeX 中的基本函数样式并进行创新推理,您可以执行以下操作:

为一些文本提供的基本功能是\myfunstuff{Hello world!}将文本着色为蓝色,并将其后跟的文本恢复为黑色。

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

这有助于您将前面的答案总结为:

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

现在介绍一个小技巧,如果您使用该命令,则无法引入换行符parbox。解决方案是添加\vspace*{.5cm} \\以模拟段落效果:

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

如果我使用 lipsum 命令,LaTeX 会抱怨没有行可以换行。因此,我从维基百科瞧!

在此处输入图片描述

答案2

该包的另一种可能性gradient-text是:

\documentclass{article}

\usepackage{gradient-text}

\begin{document}

\gradientRGB{Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.}{0,0,0}{255,255,255}

\end{document}

在此处输入图片描述

相关内容