如何实现渐变颜色文本的自动换行?

如何实现渐变颜色文本的自动换行?

我想为字行添加渐变颜色,但过长的字行却带来了麻烦。

以下是代码

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

显然这里的换行不起作用

换行不工作

我们可以看到彩色文本超出了边距的范围。

在此先感谢所有提供帮助的人。


使用时

\parbox{\textwidth}

在文本中确实有帮助。但对于同一行中具有不同颜色渐变的文本,就会出现麻烦

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

输出如下

同一行不同颜色渐变

答案1

我的意思是将 a 放入\parbox#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}

演示

答案2

这不是一个tikz解决方案,而是一种不同的方法。它基于我在仅使用文字制作烟花如何在一定长度之后“淡出”文本?. 可以成功换行。

已编辑sloppypar,现在使用\colorlet而不是\def,这允许将混合颜色指定为\FadeColors参数。将颜色指定为 后\FadeColors{<start-color>}{<end-color>},可以使用语法\FadeAfter{<length>}{content}。淡出是根据当前文本长度完成的,而不是整体应用于矩形文本框。

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

在此处输入图片描述

答案3

您可以使用该gradient-text包:

\documentclass{article}

\usepackage{gradient-text}

\begin{document}

\gradientRGB{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.}{255,165,0}{0,255,255}

\end{document}

在此处输入图片描述

相关内容