2

for demonstration purposes I'd like to set a paragraph with random wordspacing. I tried to adapt the solution for the chtulhu question (https://tex.stackexchange.com/a/29458/9666) but actually I must admit: I don't understand the given solution.

This is what I tried so far:

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

Any help?

2 Answers 2

4

You basically forgot a \fi and #2%. That is, the \ifx does not get terminated and the "consuming" not continued.

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

enter image description here

1
  • Wow, that was quick. Thank you! Jan 8, 2020 at 16:52
3

You can do it more easily with expl3.

The input is split at spaces and then between each word a random space is inserted. I used the rand() function that returns a uniformly distributed number between 0 (included) and 1 (excluded), which is then rescaled to be between 2 and 10.

I also added some amount of stretching and shrinking, which could also be made random, so to achieve justification.

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .