5

I am searching for a way to alternately change the format of words in a piece of text.

Like so: This is an short example

I thought you could step a counter after every word and format words with even numbers the one way and the rest the other way. But I have no clue how to actually do that.

Just in case you are wondering why the hell I would want to do that: I want to use this to reformat a whole book from the gutenberg project. Words should alternately be formated like described in my old thread. Long story short: I hope to speed up the process of inducing graphemic synesthesia this way.

  • What have you tried? How do you want to use this? For a whole document? For a paragraph? For a few words? – cfr Nov 26 '16 at 2:58
  • Currently I only searched for solutions, but I couldn´t find anything that comes close to my problem. – AstronAUT Nov 26 '16 at 3:14
8

This method can work on multiple paragraphs, and will always start the new paragraph in italics and then alternate with bold thereafter.

EDITED to define the two styles in user-settable macros \styleA and \styleB.

REEDITED so that \styleA doesn't have to undo \styleB and vice-versa.

\documentclass{article}
\newcounter{stylecount}
\def\styleA{\itshape}
\def\styleB{\bfseries}
\newcommand\altstyle[1]{\altstylepar#1\par\relax}
\long\def\altstylepar#1\par#2\relax{\setcounter{stylecount}{0}%
  \altstylehelp#1 \par%
  \ifx\relax#2\relax\else\par\altstylepar#2\relax\fi}
\long\def\altstylehelp#1 #2\par{%
  \stepcounter{stylecount}%
  \bgroup\ifnum\thestylecount=1\relax\styleA\else%
    \setcounter{stylecount}{0}\styleB\fi%
  #1\egroup%
  \ifx\relax#2\relax\else\ \altstylehelp#2\par\fi%
}
\begin{document}
\altstyle{This is a test.

And another test.

And finally a third paragraph.}

\end{document}

enter image description here

To use "style" macros that take an argument, this variant of the above answer would be such a way:

\documentclass{article}
\newcounter{stylecount}
\def\styleA{\textit}
\def\styleB{\textbf}
\newcommand\altstyle[1]{\altstylepar#1\par\relax}
\long\def\altstylepar#1\par#2\relax{\setcounter{stylecount}{0}%
  \altstylehelp#1 \par%
  \ifx\relax#2\relax\else\par\altstylepar#2\relax\fi}
\long\def\altstylehelp#1 #2\par{%
  \stepcounter{stylecount}%
  \bgroup\ifnum\thestylecount=1\relax\styleA{#1}\else%
    \setcounter{stylecount}{0}\styleB{#1}\fi%
  \egroup%
  \ifx\relax#2\relax\else\ \altstylehelp#2\par\fi%
}
\begin{document}
\altstyle{This is a test.

And another test.

And finally a third paragraph.}

\end{document}
  • Wow. I was just working on something based on etoolbox list processing, but I couldn't find a solution that actually worked with white space as a list separator. This is much better. If I might make one little suggestion, it would be to split out the definitions of the alternating formats, so that one does not have to delve into the middle of things to change them. – Michael Palmer Nov 26 '16 at 3:40
  • 1
    @MichaelPalmer I have broken out those definitions as \styleA and \styleB by way of EDIT. – Steven B. Segletes Nov 26 '16 at 3:47
  • That works well, but how do I use a macro (like \textit{}) instead of an environment (like itshape) to define the style? – AstronAUT Nov 26 '16 at 4:31
  • @AstronAUT \itshape is a macro. It just doesn't take an argument. – cfr Nov 26 '16 at 4:42
  • ohh, did not know that. Then how do I use a macro that takes text as an argument? – AstronAUT Nov 26 '16 at 4:45
8

A Plain TeX solution. THanks to Steven Segletes for showing me how to wrap the catcode assignment in a command.

\newcount\bold
\bgroup
\catcode`\ =\active
\gdef {\rm\ifnum\bold=1\bf\bold=0\else\it\bold=1\fi\ }
\egroup
\def\funkyon{\bgroup\bf\catcode`\ =\active}
\def\funkyoff{\egroup}

\funkyon

This is funky format text with alternating bold and italic.
We built a state machine and then turned the space character
into a command that toggles the state values.

\funkyoff

more text
\bye
  • 1
    That's a pretty inventive approach. +1 – Steven B. Segletes Nov 26 '16 at 3:48
  • Is \mdseries a plain TeX command? – Steven B. Segletes Nov 26 '16 at 3:53
  • I hope you don't mind my edit of your answer. – Steven B. Segletes Nov 26 '16 at 4:11
  • 2
    @StevenB.Segletes Thanks for the assist! I incorporated your approach and optimized the code a bit. – musarithmia Nov 26 '16 at 4:17
7

This is based on David Carlisle's answer to your earlier question.

It can handle paragraphs and offers key-value customisation. My approach is based on three styles rather than two: a common style applied before each of the init and alt styles. This makes it possible to define the common style to be empty, so the changes are cumulative, or to reset everything in between. By default, common resets both font style and size, init uses italics and alt uses bold.

This can be changed globally and/or locally.

Syntax:

  • \astroalt[<key-value options>]{<text>}
  • \astroaltset{<key-value options>}

where the available keys are:

  • init=<font switches>
  • alt=<font switches>
  • common=<font switches>

init sets the style of the initial word and every other thereafter. alt sets the style of alternate words. common is applied to all words before the styles set by init or alt.

Use \astroaltset{} to set default values indefinitely. Use the optional argument to \astroalt[]{} to set one-off changes.

% addaswyd o ateb David Carlisle: https://tex.stackexchange.com/a/213427/
\documentclass{article}
\usepackage{xparse,kantlipsum}

\ExplSyntaxOn
\NewDocumentCommand \astroalt { o +m }
{
  \IfValueT { #1 }
  {
    \keys_set:nn { astro / alting } { #1 }
  }
  \astro_alt:n { #2 }
}
\NewDocumentCommand \astroaltset { m }
{
  \keys_set:nn { astro / alting } { #1 }
}
\keys_define:nn { astro / alting }
{
  alt .tl_set:N = \l_astro_alt_tl,
  alt .initial:n = { \itshape },
  common .tl_set:N = \l_astro_common_tl,
  common .initial:n = { \normalfont\normalsize },
  init .tl_set:N = \l_astro_init_tl,
  init .initial:n = { \bfseries },
}
\tl_new:N \l_astro_alternate_tl
\bool_new:N \l_astro_alt_bool
\bool_set_false:N \l_astro_alt_bool
\cs_set_protected:Npn \astro_alt:n #1
{
  \group_begin:
  \bool_set_false:N \l_astro_alt_bool
  \astro_format:
  \tl_set:Nn \l_astro_alternate_tl { #1 }
  \tl_replace_all:Nnn \l_astro_alternate_tl {~} { ~ \astro_format: }
  \tl_use:N \l_astro_alternate_tl
  \group_end:
}
\cs_new_protected:Nn \astro_format:
{
  \bool_if:NTF \l_astro_alt_bool
  {
    \l_astro_common_tl
    \l_astro_alt_tl
    \bool_set_false:N \l_astro_alt_bool
  }
  {
    \l_astro_common_tl
    \l_astro_init_tl
    \bool_set_true:N \l_astro_alt_bool
  }
}
\ExplSyntaxOff
\newcommand\kanttest{%
  \astroalt{% from kantlipsum
    As any dedicated reader can clearly see, the Ideal of practical reason is a representation of, as far as I know, the things in themselves;
    as I have shown elsewhere, the phenomena should only be used as
    a canon for our understanding. The paralogisms of practical reason
    are what first give rise to the architectonic of practical reason. As
    will easily be shown in the next section, reason would thereby be
    made to contradict, in view of these considerations, the Ideal of practical reason, yet the manifold depends on the phenomena. Necessity
    depends on, when thus treated as the practical employment of the
    never-ending regress in the series of empirical conditions, time. Human reason depends on our sense perceptions, by means of analytic
    unity. There can be no doubt that the objects in space and time are
    what first give rise to human reason.

    Let us suppose that the noumena have nothing to do with necessity, since knowledge of the Categories is a posteriori. Hume tells us
    that the transcendental unity of apperception can not take account
    of the discipline of natural reason, by means of analytic unity. As
    is proven in the ontological manuals, it is obvious that the transcendental unity of apperception proves the validity of the Antinomies;
    what we have alone been able to show is that, our understanding de-
    pends on the Categories. It remains a mystery why the Ideal stands
    in need of reason. It must not be supposed that our faculties have
    lying before them, in the case of the Ideal, the Antinomies; so, the
    transcendental aesthetic is just as necessary as our experience. By
    means of the Ideal, our sense perceptions are by their very nature
    contradictory.
  }%
}
\begin{document}

\astroalt{%
  Here is some text.

  Here is some more.%
}

\astroalt[alt=\scshape]{%
  Here was some stuff.

  But there is no more.%
}

\astroaltset{%
  init=\sffamily,
  alt=\footnotesize,
  common=\normalfont\normalsize\bfseries,
}

\kanttest


\end{document}

alternating styles for words

  • Thank you so much for your effort, if I could only accept all your answers! – AstronAUT Nov 26 '16 at 4:54

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.