I am trying to create a document class with random style attributes, determined at compilation time. Random attributes would ideally include:

  • Margins
  • Line spacing
  • Font selection

For my particular use case, in-document references are not typically useful/a design constraint, so there's no first-order need to worry about layout being consistent across compilations.

I attempted to use \pgfmathrandomitem in a new .cls file, as follows:

% randocls.cls


% none of the following work...
% (the implied margins are different; this is MWE so nbd)

% Case 1:

% Case 2:
% \addtolength{\topmargin}{{\marginns}in}

% Case 3:
% \let\rmarginns{\marginns}
% \addtolength{\topmargin}{{\rmarginns}in}

Under Case 1:

("C:\Program Files\MiKTeX 2.9\tex/latex/geometry\geometry.cfg")
! Missing number, treated as zero.
<to be read again> 
l.1007 \ProcessOptionsKV[p]{Gm}

Under Case 2:

! Missing number, treated as zero.
<to be read again> 
l.23 \addtolength{\topmargin}{{\marginns}in}

Under Case 3:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.

l.30 \let\rmarginns{\marginns

My best guess is that the problem relates to when pgfmathrandomitem can actually yield a random item, but I am in over my head. Is there a way to work around this, or another randomization mechanism that will allow randomization in the document header?

  • 2
    Note that the random seed is based on the minutes of the system clock. See tex.stackexchange.com/questions/144621/… if you want to have a new random seed more often. – Marijn May 2 '19 at 13:54
  • @Marijn this is helpful. I'm going to be using XeTeX for font selection, so the linked solution will not work. However, outside of testing it's really not a big deal to have the random seed changed only once per minute. – kyle May 2 '19 at 14:25

There is some issue with expansion and groups - this is always tricky in LaTeX. A solution here is to put the unit in the list:

% randocls.cls


% Case 1:
margin: \marginns

enter image description here

  • Great, this solves the margins issue; not as difficult as I thought! I'm going to make sure it solves the other issues (spacing, font selection) before accepting. – kyle May 2 '19 at 14:24
  • The one note to add: because I would like to select different left-right and top-bottom margins, it's necessary to select two different random lengths. Due to pgf's innards, I need to select a random margin, edef it to a temporary variable, then select another random margin; otherwise, the same random margin is returned twice. – kyle May 2 '19 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.