每行文本渐变(ala beeline reader)

每行文本渐变(ala beeline reader)

我看过 Beeline 阅读器的演示,想在我的书籍实体版(使用 LaTeX 编写)上测试一下。我想知道这是否可行。

Beeline 阅读器进行在线测试,在一行文本的字母上添加轻微渐变。在下一行,它会反转渐变的方向。这样做的目的是帮助跟踪文本并提高阅读速度。

关于如何在 LaTeX 中执行此操作有什么提示或建议吗?

以下是取自http://www.beelinereader.com/

enter image description here

答案1

感谢

如何将颜色渐变设置为“仅所需文本”

enter image description here

\documentclass{article}
\usepackage{lipsum}


\usepackage{tikz}
\usetikzlibrary{fadings,patterns}
\begin{document}

\newcommand\tikzsection[1]{%
  \begin{tikzfadingfrompicture}[name=tikzsection]
    \node [text=white] {#1};
  \end{tikzfadingfrompicture}
    \begin{tikzpicture}
      \node [text=white,inner sep=0pt,outer sep=0pt] (textnode) {#1};
      \shade[path fading=tikzsection,fit fading=false,left color=\lcolor,right color=\rcolor]
      (textnode.south west) rectangle (textnode.north east);
    \end{tikzpicture}%
}

\makeatletter
\setbox1=\vbox{}


\def\zcolor{red}

\setbox2\vbox{\def\write#1#2{}%
\section{Grr dasadsa}
\lipsum\par


% at this point we are at the end of box 2 now
% loop backwards up the vertical list copying glue and penalties
% but modifying boxesto add the prefix.
\loop
%
% an e-tex primitive that reports the last item in the vertical list.
% type 1 is a box, so remove the box (a lien of text) and then
% put into box 1 a box that is this box together with the prefix which
% was saved in box 0
\ifnum\lastnodetype=1
\ifx\rcolor\zcolor
\gdef\lcolor{red}\gdef\rcolor{blue}%
\else
\gdef\lcolor{blue}\gdef\rcolor{red}%
\fi
\global\setbox3\lastbox
\global\setbox1\vbox{%
\hbox{\tikzsection{\copy\thr@@}}%
\unvbox1}%
\fi
%
% type 11 is glue so remove it from this list and add
% equivalent glue to box 1
\ifnum\lastnodetype=11
\skip0\lastskip\unskip
\global\setbox1\vbox{\vskip\skip0\unvbox1}%
\fi
%
% same for penalty
\ifnum\lastnodetype=13
\count0\lastpenalty\unpenalty
\global\setbox1\vbox{\penalty\count0 \unvbox1}%
\fi
%
% other node types are not handled here: either they won't happen
% or they can't be removed anyway so would break the loop \special
% for example would be bad.
%
% -1 means the vertical list is empty: we have reached the top of the box.
\ifnum\lastnodetype=-1
\else
\repeat}%


% Tip the modified box back onto the main list for the page, unbox
% it so that page breaking may still happen (if it could happen in the
% original context).
\unvbox1




\end{document}

修改后的颜色循环更接近问题中添加的图像,可以添加

\newcount\colorcycle

到序言中,然后修改框节点类型代码如下:

\ifnum\lastnodetype=1
\ifcase\colorcycle
\gdef\lcolor{red}\gdef\rcolor{black}\or
\gdef\lcolor{black}\gdef\rcolor{red}\or
\gdef\lcolor{blue}\gdef\rcolor{black}\or
\gdef\lcolor{black}\gdef\rcolor{blue}\global\colorcycle\m@ne\fi
\global\advance\colorcycle\@ne
\global\setbox3\lastbox
\global\setbox1\vbox{%
\hbox{\tikzsection{\copy\thr@@}}%
\unvbox1}%
\fi

enter image description here

答案2

这是 Luatex 的一个绝妙用例!(事实上,当我昨天阅读有关蜜蜂颜色的文章时,我希望这个问题会出现在 TeX-SE 上。)所以,事不宜迟, 这是必要的代码. 这要求Luaotfload,Latex 用户无需明确加载,如果他们正在使用字体规格包裹。

beecolors 代码分为 TeX 文件和 Lua 文件。Lua 部分 ( beegradients.lua) 为回调实现了一个节点处理器 post_linebreak_filter。其原理非常简单:它扫描构成段落的水平列表,查找字形和连字符,并用适当的 PDF 颜色内容包围这些字形和连字符,并递归到沿途遇到的任何 vlist 和 hlist。

第二个文件(beegradients.tex)包含用于定义渐变和切换回调的包装器宏。渐变组是颜色表达式的列表,可以像这样定义:

\definegradientgroup [<name>][<col1>,<col2>, ... ,<coln>]

例如

\definegradientgroup [blackwhite][0x000000, 0xFFFFFF]

安装由两种颜色(黑色和白色)组成的渐变列表。同样,

\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]

定义三种颜色的列表:红色、绿色和蓝色。

这些渐变组随后可以通过其名称来引用。回调可以通过环境激活 蜜蜂梯度

\startbeegradients [<name>]
  ... ... ...
\stopbeegradients

其中<name>指的是先前定义的渐变组。例如,为了使用组黑,白红绿蓝我们上面定义:

\startbeegradients [blackwhite]
  \input knuth
\stopbeegradients

\startbeegradients [red-green-blue]
  \input knuth
\stopbeegradients

如果没有可选参数,\startbeegradients则将选择最后一个活动组。

\startbeegradients [blackwhite]
  \input knuth
\stopbeegradients

foo bar baz       %% <- no gradient

\startbeegradients
  \input knuth    %% <- black and white again
\stopbeegradients

以下是 Plain 格式的完整示例,其中显示了几个定义:

\input luaotfload.sty
\input beegradients.tex

\font \mainfont = file:Iwona-Regular.otf at 10pt
\mainfont

\definegradientgroup [mygradients][
  42*11*242,        %% decimal notation, separated by “*”
  83*242*55,
  0xf00ba7,         %% hex notation
  0x1ec001,
  g:23*b:42*r:133,  %% rgb notation, also separated by “*”
  b:53*g:184*r:10,
]

\definegradientgroup     [blackwhite][0x000000, 0xFFFFFF]
\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]
\definegradientgroup            [red][255*0*0, 0*0*0]
\definegradientgroup          [green][0*100*0, 0*255*0]
\definegradientgroup           [blue][0*0*20, 0*0*210]

\input knuth

\startbeegradients [mygradients]    \input knuth \stopbeegradients
\startbeegradients [blackwhite]     \input knuth \stopbeegradients
\startbeegradients [red-green-blue] \input knuth \stopbeegradients
\startbeegradients [red]            \input knuth \stopbeegradients
\startbeegradients [green]          \input knuth \stopbeegradients
\startbeegradients [blue]           \input knuth \stopbeegradients

\bye

结果:

Colorful Plain TeX document

当然代码与Latex兼容,你可以直接加载它:

\documentclass {scrartcl}
\usepackage {fontspec} %% this loads luaotfload as well
\setmainfont {Antykwa Poltawskiego}
\input beegradients.tex

\definegradientgroup [red-green-blue][255*0*0, 0*255*0, 0*0*255]

\begin {document}
  \startbeegradients [red-green-blue]
    \input knuth
  \stopbeegradients
\end {document}

这就引出了下面这份丰富多彩的文件:

Colorful Latex document

对于 texlive 2016,请参阅此帖子:https://tex.stackexchange.com/a/321962/90087

相关内容