![如何在一定长度之后“淡出”文本?](https://linux22.com/image/299327/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%80%E5%AE%9A%E9%95%BF%E5%BA%A6%E4%B9%8B%E5%90%8E%E2%80%9C%E6%B7%A1%E5%87%BA%E2%80%9D%E6%96%87%E6%9C%AC%EF%BC%9F.png)
我有一段很长的文本,但报告中只能容纳一行。一旦到达,我怎样才能淡出它.5\linewidth
?
我想要一个类似于
\FadeAfter{.5\linewidth}{Quack, quack}
随机要求
- PDF 格式
- 它需要从 Evince 2.28.2(不要攻击信使)和最新版本的 Adobe Reader 正确查看和打印
有关的
答案1
以下是在文本上绘制白色的可能性:
\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{fadings}
\makeatletter
\tikzfading[name=fade left,
left color=transparent!100,
right color=transparent!0]
\newcommand{\FadeAfter}[2]{%
\par\noindent\begin{tikzpicture}
\node[inner sep=0pt,inner ysep=2pt,outer sep=0pt,clip] (A) {\makebox[\linewidth][l]{#2}};
\fill[white,path fading=fade left] ([xshift={#1}]A.south west) rectangle ([xshift={1pt}]A.north east);
\end{tikzpicture}\par%
}
\makeatother
\begin{document}
\FadeAfter{0pt}{\lipsum[1]}
\FadeAfter{.25\linewidth}{\lipsum[1]}
\FadeAfter{.5\linewidth}{\lipsum[1]}
\FadeAfter{.75\linewidth}{\lipsum[1]}
\medskip
\lipsum[1]
\end{document}
我已经删除了缩进,但是如果您想缩进文本,可以调整宏。
答案2
这里的策略是每次循环添加一个字符,将当前字符串塞入一个框中并测量它。然后,使用当前长度的分数度量来淡化长度,以从该字符的颜色中减去强度。\prefahelper
我使用例程来确定我是否处于单词末尾并需要记住一个空格。该例程\fahelper
是主力,由 递归调用\fahelp
。
感谢 percusse划分维度以获得计数展示如何使用计数器划分长度(如果我没记错的话,这是他从 egreg 那里学到的一个技巧)。
已编辑,将分母乘以 0.01,而不是将分子乘以 100,以避免计数器溢出。显示多行示例。已编辑,以纠正单字母单词的错误。
\documentclass{article}
\usepackage{xcolor,ifthen}
\newcounter{tmpcounter}
\newlength\cumlength
\newlength\critlength
\newlength\tmplength
\newcount\mynum
\newcount\myden
\makeatletter
\newcommand\FadeAfter[2]{\critlength=#1\relax\cumlength=0pt\relax%
\def\cumstring{}\fahelp{#1}{#2}}
\newcommand\fahelp[2]{\prefahelper#2 \relax\fahelper#2\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{black!\thetmpcounter}{#1}%
\ifthenelse{\equal{#1}{\wordremaining}}{\ }{}%
\ifdim\wd0<\critlength%
\ifx\relax#2\relax\else\fahelp{\critlength}{#2}\fi%
\fi%
}
\makeatother
\begin{document}
\FadeAfter{.5in}{testing whether this fades away}%
\FadeAfter{1in}{testing whether this fades away}%
\FadeAfter{1.5in}{testing whether this fades away}%
\FadeAfter{2.5in}{testing whether this fades away}
\FadeAfter{.5in}{testing whether this fades away}\par
\FadeAfter{1in}{testing whether this fades away}\par
\FadeAfter{1.5in}{testing whether this fades away}\par
\FadeAfter{2.5in}{testing whether this fades away}\par
\FadeAfter{7in}{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 This is a very long test}
\end{document}