9

I am currently working on a technical report using pdflatex and noticed that I overuse the term "w.r.t." (at least I wrote a command for it).

Is there a way to write a macro that randomly chooses "x", where "x in {with respect to; concerning; with regard to; ...}" such that I do not have to replace each instance of my command \wrt manually in the document?

PS: I tried using the search function, but did not find anything suitable. If there is, I am fine with a polite redirection. :)

2
  • Are you forced to use pdfLaTeX, or could you switch to LuaLaTeX?
    – Mico
    Mar 27, 2017 at 5:44
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Mar 27, 2017 at 8:20

6 Answers 6

11

Here's a very simple solution that doesn't rely on external packages:

\documentclass{article}

\makeatletter
\newcount\wrt@counter
\newcommand\wrt{%
    \begingroup
    \def\alts{3}%  <-- the number of alternatives
    \count0=\numexpr17*\pagetotal/7+\wrt@counter\relax
    \global\wrt@counter=\numexpr\wrt@counter+1\relax
    \count0=\numexpr\count0-\count0/\alts*\alts\relax
    \ifcase\count0 with respect to\or concerning\else with regard to\fi
    \endgroup
}
\makeatother

\begin{document}

xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx \wrt\ 
xxxx xxx xx xxxxxxx xxxx xxx xx xxxxxxx
\wrt\par
\wrt\par
\wrt\par
\wrt\par
\wrt\par
\wrt\par
\wrt\par
\wrt\par
\wrt\par

\end{document}

enter image description here

The phrase is chosen by taking the current value of \pagetotal, i.e. the current height of all page contents, converting it into an integer (with some factors to add more randomness) and finally using this number modulo the number of phrase alternatives.

Besides it's simplicity, it should give the same results across several builds in most cases.

EDIT: I added an additional global counter such that muliple occurences of \wrt on the same line or paragraph still yield varying results.

2
  • 1
    The problem is that if you are in one (possibly very long) paragraph with more occurrences of \wrt, then all of them give the same result because \pagetotal is not changed inside paragraph.
    – wipet
    Mar 27, 2017 at 6:23
  • You are right, thanks. I fixed this problem in the code by adding a global counter.
    – siracusa
    Mar 27, 2017 at 6:56
8

If you are using TikZ (or pgf), here is a simple solution using the math functions of pgf. Simply use pgfmathdeclarerandomlist to declare a list of possible choices, then pgfmathrandomitem to choose one entry from that list at random. For more details, refer to the pgf documentation.

\documentclass{article}
\usepackage{tikz}%
\pgfmathdeclarerandomlist{words}{%
  {one}%
  {two}%
  {three}%
  {four}%
}%

\begin{document}

Here is the first randomly chosen word: \pgfmathrandomitem{\word}{words} \word

Here is the second randomly chosen word: \pgfmathrandomitem{\word}{words} \word

And a third one: \pgfmathrandomitem{\word}{words} \word


\end{document}
7

Here's a LuaLaTeX-based solution. It sets up a LaTeX macro, called \wrt, which invokes a Lua function which, in turn, randomly picks off and prints one of the entries from a table that contains all possible phrases.

enter image description here

% !TeX program = lualatex
\documentclass{article}

%% Lua-side code: (a) Table with all possible phrases ('nn' entries)
%%                (b) Function that randomly picks off and prints a phrase
\directlua{
    wrt_choices = { "with respect to",
                    "concerning",
                    "with regard to" }
    nn=0; for _ in pairs(wrt_choices) do nn=nn+1 end
    function wrt_print ()
        tex.sprint ( wrt_choices[math.random(nn)] )
    end
}   

%% LaTeX-side code: Macro that invokes the Lua function "wrt_print"
\newcommand\wrt{\directlua{wrt_print()}}

\begin{document}
\wrt, \wrt, \wrt, \wrt, \wrt
\end{document}
2
  • Why not wrt_choices[math.random(#wrt_choices)]? It’s better that hardcoding the the number 3. Mar 27, 2017 at 7:06
  • @HenriMenke - Thanks for this. I'm afraid #wrt_choices doesn't work here because the table entries' indexes aren't necessarily integers. What works, though, is to execute nn=0; for _ in pairs(wrt_choices) do nn=nn+1 end immediately after defining the table, and then using nn in the argument of math.random. I'll update the code accordingly. For more on this topic see, e.g., How to get number of entries in a Lua table?
    – Mico
    Mar 27, 2017 at 7:35
7

Here's the mandatory expl3 version; note the compactness of the code:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\newrandomtext}{mm}
 { % #1 = macro name, #2 = texts (comma separated)
  \seq_new:c { g_nils_randomtext_#1_seq }
  \cs_new:cpn { #1 } { \seq_rand_item:c { g_nils_randomtext_#1_seq } }
  \seq_gset_from_clist:cn { g_nils_randomtext_#1_seq } { #2 }
 }

\ExplSyntaxOff

\newrandomtext{wrt}{
  with respect to,
  concerning,
  with regard to
}

\begin{document}

\wrt{} \wrt{} \wrt{} \wrt{}
\wrt{} \wrt{} \wrt{} \wrt{}

\end{document}

Note At the moment the code doesn't work with XeTeX. A (non expandable) workaround is possible, still keeping the code compact.

enter image description here

1
  • With TeX Live 2019, the code also works with XeLaTeX.
    – egreg
    Sep 1, 2019 at 15:35
3

You can use lcg to create random numbers and \ifcase to choose a text depending on the random numbers:

\documentclass{article}
\usepackage[first=0, last=2]{lcg}
% \rand generates new random integer, \therand returns it
\newcommand{\wrt}{%
    \rand
    \ifcase\therand
        with respect to%
    \or
        concerning%
    \else
        with regard to%
    \fi
}

\begin{document}
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
\end{document}
2

You can create several macros and choose one of them with a random number produced by lcg:

\documentclass{article}
\usepackage[first=0, last=2]{lcg}
\expandafter\newcommand\csname wrt0\endcsname{with respect to}
\expandafter\newcommand\csname wrt1\endcsname{concerning}
\expandafter\newcommand\csname wrt2\endcsname{with regard to}
% \rand generates new random integer, \therand returns it
\newcommand{\wrt}{\rand\csname wrt\therand\endcsname}

\begin{document}
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
    \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ \wrt\ 
\end{document}

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