Debian Codegraph @

texmf-dist/doc/latex/gradient-text/gradient-text.tex

raw
1 \documentclass{ltxdoc}
2 \title{The \textsf{\gradientRGB{gradient-text}{255,63,63}{6,60,255}} package\footnote{This project is under the \LaTeX~Project Public License.}}
3 \usepackage{listings}
4 \lstset{
5 basicstyle=\fontspec{Consolas},numbers=left,
6 numberstyle=\small,stepnumber=1,numbersep=5pt
7 }
8 \usepackage{indentfirst}
9 \usepackage{fontspec}
10 \setmainfont{Times New Roman}
11 \usepackage{tcolorbox}
12 \usepackage[misc,geometry]{ifsym}
13 \author{Sicheng Du\thanks{\Letter~~\href{mailto:[email protected]}{\gradientRGB{[email protected]}{0,224,238}{173,0,254}}}}
14 \date{\today~~~~v1.2}
15 \usepackage[colorlinks,linkcolor=purple]{hyperref}
16 \usepackage{gradient-text}
17 \begin{document}
18 \maketitle
19 \section{Changes in this version}
20 Thanks to the \TeX nicians who kindly gave valuable and earnest suggestions to this package, the version 1.2 is now released. The main changes include
21 \begin{enumerate}
22 \item Made the syntax of the user command more convenient;
23 \item Modified the format in the source code to improve compatibility;
24 \end{enumerate}
25 \section{User instructions}
26 The \textsf{gradient-text} package enables writers to conveniently decorate text with linear gradient colors. It's effect can be seen from the title.
27
28 \begin{macro}{\gradientRGB}
29 \marg{text}\marg{first RGB}\marg{last RGB}
30 This is the syntax of the command for putting gradient color on text . It takes three mandatory arguments.
31 \end{macro}
32 \begin{description}
33 \item[\marg{text}] is the text you desire to have gradient color. Be aware that this command most likely wouldn't output content as expected if this parameter is filled with tokens that cannot expand into pure text. Below are two examples.
34 \begin{tcolorbox}[sidebyside,coltitle=black,colbacktitle=white,lower separated=false,title=A correct usage,oversize,boxrule=0.5pt]
35 \begin{verbatim}
36 \newcommand{\hello}{Hello!}
37 \gradientRGB{\hello}{0,255,0}{255,0,0}
38 \end{verbatim}
39 \tcblower
40 \hfill\gradientRGB{Hello!}{0,255,0}{255,0,0}
41 \end{tcolorbox}
42 \begin{tcolorbox}[sidebyside,coltitle=orange,colbacktitle=white,lower separated=false,title=\SmallCross~An incorrect usage,oversize,boxrule=0.5pt]
43 \begin{verbatim}
44 \newcommand{\hello}{\textit{Hello!}}
45 \gradientRGB{\hello}{0,255,0}{255,0,0}
46 \end{verbatim}
47 \tcblower
48 \hfill\gradientRGB{\textit{Hello!}}{0,255,0}{255,0,0}
49 \end{tcolorbox}
50 For this wrong example, the intended result can be obtained by
51 \begin{quote}
52 \verb|\textit{\gradientRGB{hello!}{0,255,0}{255,0,0}}|
53 \end{quote}
54 just like how I created the title by typing
55 \begin{quote}
56 \verb|\textsf{\gradientRGB{gradient-text}{255,63,63}{6,60,255}}|
57 \end{quote}
58
59 In addition, for any space characters in \marg{text}, they will not be take a unique color, although they will still show up in their original position.
60 \begin{tcolorbox}[coltitle=black,colbacktitle=white,lower separated=false,title=Example of space characters,oversize,boxrule=0.5pt]
61 \verb|\gradientRGB{Hello World}{0,255,0}{255,0,0}|\hfill\gradientRGB{Hello World}{0,255,0}{255,0,0}
62 \tcblower
63 \verb|\gradientRGB{HelloWorld}{0,255,0}{255,0,0}|\hfill\gradientRGB{HelloWorld}{0,255,0}{255,0,0}
64 \end{tcolorbox}
65 \item[\marg{first RGB}] specifies the RGB value given to the first character of the \marg{text}. It should be formatted as a three-item comma list in brackets \verb|{}|, of which each item is a natural number not exceeding 255.
66
67 All of \verb|{13,28,176}|, \verb|{0,59,2}| and \verb|{255,34,5}| are correct examples.
68 \item[\marg{last RGB}] is similar to \marg{first RGB} except that it controls the RGB color of the last character in the \marg{text}.
69 \end{description}
70
71 \emph{\gradientRGB{Notice.}{11,45,14}{191,98,10}} The syntax of \marg{first RGB} and \marg{last RGB} have changed. Version 1.1 required an additional pair of curly brackets.
72
73 \section{Code implementation}
74 Here is the heading,
75 \begin{lstlisting}[firstnumber=4]
76 \NeedsTeXFormat{LaTeX2e}
77 \ProvidesPackage{gradient-text}[2022/12/24]
78 \ExplSyntaxOn
79 \end{lstlisting}
80
81 Then we define some variables
82 \begin{lstlisting}[firstnumber=last]
83 \clist_new:N\l_gtext_FirstRGB_clist
84 \clist_new:N\l_gtext_LastRGB_clist
85 \int_new:N\l_gtext_MaxIndex_int % max index (detach spaces)
86 \int_new:N\l_gtext_Ratio_int
87 \end{lstlisting}
88
89 Now we define the internal command \cs{gr@dientRGB}
90 \begin{lstlisting}[firstnumber=last,breaklines=true]
91 \newcommand{\gr@dientRGB}[7]{
92 \int_set:Nn\l_gtext_MaxIndex_int{\int_eval:n{\str_count:n{#1}}}
93 \int_step_inline:nnn{1}{\l_gtext_MaxIndex_int}{
94 \exp_args:Ne\str_if_eq:nnTF{\str_item:Nn{#1}{##1}}{~}{}{
95 \int_set:Nn\l_gtext_Ratio_int{\int_eval:n{\l_gtext_Ratio_int+1}}
96 }
97 \color_select:nn{RGB}{
98 \int_eval:n{(\int_use:N\l_gtext_Ratio_int*#5+(\l_gtext_MaxIndex_int-##1)*#2)/\l_gtext_MaxIndex_int},
99 \int_eval:n{(\int_use:N\l_gtext_Ratio_int*#6+(\l_gtext_MaxIndex_int-##1)*#3)/\l_gtext_MaxIndex_int},
100 \int_eval:n{(\int_use:N\l_gtext_Ratio_int*#7+(\l_gtext_MaxIndex_int-##1)*#4)/\l_gtext_MaxIndex_int}
101 }\str_item:Nn{#1}{##1}
102 }
103 }
104 \end{lstlisting}
105
106 Lastly we define the user command. The \textsf{l3clist} package is used to interpret the parameters into three integers.
107 \begin{lstlisting}[firstnumber=last]
108 \NewDocumentCommand\gradientRGB{mmm}{{
109 \clist_set:Nn\l_gtext_FirstRGB_clist {#2}
110 \clist_set:Nn\l_gtext_LastRGB_clist {#3}
111 \gr@dientRGB{#1}
112 {\clist_item:Nn\l_gtext_FirstRGB_clist{1}}
113 {\clist_item:Nn\l_gtext_FirstRGB_clist{2}}
114 {\clist_item:Nn\l_gtext_FirstRGB_clist{3}}
115 {\clist_item:Nn\l_gtext_LastRGB_clist{1}}
116 {\clist_item:Nn\l_gtext_LastRGB_clist{2}}
117 {\clist_item:Nn\l_gtext_LastRGB_clist{3}}
118 }}
119 \ExplSyntaxOff
120 \end{lstlisting}
121 \end{document}