# tabular environment with random positions

I would like to simulate a simple random cloud of letters using a custom tabular environment, like this:

\documentclass{article}
\usepackage{lcg}
\begin{document}

\reinitrand[first=12,last=20,counter=n]
\begin{tabular}[t]{*3{>{\rand\fontsize{\then}{\then}\selectfont}c}}
X & X & X \\
X & X & X \\
X & X & X \\
\end{tabular}

\end{document}


In addition, I also want to relocate "cells" in a similar way, but it is not possible because the syntax of the tabular environment supports declarations but not commands like \put(,){}

In other words, I would like to replace the \put command by any declaration inside the tabular preamble, in order to avoid this redundant syntax:

\newcount\x
\newcount\y
\newcommand{\rndpos}[1]{
\rand\x\then
\rand\y\then
\put(\x,\y){#1}
}

\reinitrand[first=0,last=4,counter=n]
\begin{tabular}[t]{*3{>{\rand\fontsize{\numexpr 3*\then+10}{\then}\selectfont}c}}
\rndpos{X} & \rndpos{X} & \rndpos{X} \\
\rndpos{X} & \rndpos{X} & \rndpos{X} \\
\rndpos{X} & \rndpos{X} & \rndpos{X} \\
\end{tabular}


Use collcell. Also, see How to execute command on every table column

\documentclass{article}
\usepackage{lcg}
%This friend makes the magic
\usepackage{collcell}
%Define a column type so the cell's content is passed as an argument to \rndpos. We can also put the random font size commands here
\newcommand{\rndsz}{%
\rand\fontsize{\numexpr 3*\then+10}{\then}\selectfont%
}
\newcolumntype{Z}{>{\rndsz\collectcell\rndpos}{c}<{\endcollectcell}}
\newcount\x
\newcount\y
\newcommand{\rndpos}[1]{
\rand\x\then
\rand\y\then
\put(\x,\y){#1}
}

\begin{document}
\reinitrand[first=0,last=4,counter=n]
\begin{tabular}[t]{*3{Z}}
X & X & X \\
X & X & X \\
X & X & X \\
\end{tabular}
\end{document}


No need to define new environments, I think.

• Why do you still use \rndpos{X} in the cell entry when you are using the W column type to apply \rndpos? Is this nested usage by design? – Peter Grill Jan 7 '20 at 11:25
• Sorry, I'm gonna edit that. – user193767 Jan 7 '20 at 11:28
• You should consider using a different letter than W as new column type. If you for some reasons load the array package, you will receive an error message, because W is one of the reserved column types. – Sveinung Jan 7 '20 at 11:31

As a complement to JairoAraujo’s fine answer (upvoted), here's a simpler implementation using expl3.

\documentclass{article}
\usepackage{xparse,collcell}
\usepackage{lmodern}

\ExplSyntaxOn
\NewDocumentCommand{\randomsizeandpos}{m}
{
\fontsize{\int_rand:nn{12}{20}}{0}\selectfont
\put(\int_rand:nn{0}{4},\int_rand:nn{0}{4}){#1}
}
\ExplSyntaxOff

\begin{document}

\begin{tabular}{ *{3}{>{\collectcell\randomsizeandpos}c<{\endcollectcell}} }
X & X & X \\
a & b & c \\
X & X & X \\
\end{tabular}

\end{document}