随机字间距

随机字间距

为了演示目的,我想设置一个具有随机字距的段落。我尝试调整 chtulhu 问题的解决方案(https://tex.stackexchange.com/a/29458/9666),但实际上我必须承认:我不理解给出的解决方案。

这是我迄今为止尝试过的:

\documentclass[10pt]{article}% This is a document class providing more font size options

\usepackage[first=2,last=6]{lcg}
\newcommand{\globalrand}{\rand\arabic{rand}pt}
\newcommand{\randomskip}[1]{\spaceskip=\globalrand}

\makeatletter
\def\randwordspace#1{%
    \@randwordspace#1 \@empty
}
\def\@randwordspace#1 #2{%
   \randomskip{#1}\space
   \ifx #2\@empty\else
    \expandafter\@randwordspace
}
\makeatother
% ----------



\begin{document}\pagestyle{empty}

\randwordspace{This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.} 

\end{document}

有什么帮助吗?

答案1

您基本上忘记了\fi#2%。也就是说,\ifx不会终止,并且“消费”不会继续。

\documentclass[10pt]{article}% This is a document class providing more font size options

\usepackage[first=2,last=6]{lcg}
\newcommand{\randomhspace}{\rand\hspace{\the\dimexpr\value{rand}pt}}

\makeatletter
\def\randwordspace#1{%
    \@randwordspace#1 \@empty
}
\def\@randwordspace#1 #2{%
   #1\randomhspace
   \ifx #2\@empty\else
    \expandafter\@randwordspace
   \fi
   #2%
}
\makeatother
% ----------



\begin{document}
\pagestyle{empty}

\randwordspace{This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.
    This is a paragraph I would like to have set with random spaceskip between the words.} 

\end{document}

在此处输入图片描述

答案2

您可以使用 更轻松地完成此操作expl3

输入以空格分隔,然后在每个单词之间插入一个随机空格。我使用了rand()返回 0(包括)和 1(不包括)之间均匀分布的数字的函数,然后将其重新缩放到 2 到 10 之间。

我还添加了一些拉伸和收缩,也可以使其随机,从而实现合理性。

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\randwordspace}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
  \seq_use:Nn \l_tmpa_seq { \hspace{\fp_eval:n { 8*rand()+2 }pt plus 2pt minus 1pt} }
 }

\ExplSyntaxOff


\begin{document}

\randwordspace{
  This is a paragraph I would like to have set with random spaceskip between the words.
  This is a paragraph I would like to have set with random spaceskip between the words.
  This is a paragraph I would like to have set with random spaceskip between the words.
  This is a paragraph I would like to have set with random spaceskip between the words.
  This is a paragraph I would like to have set with random spaceskip between the words.
}

\end{document}

在此处输入图片描述

相关内容