我正在寻找一种方法来交替改变一段文本中单词的格式。
就像这样:这 是 一个 短的 例子
我以为你可以在每个单词后设置一个计数器,并以一种方式格式化带有偶数的单词,以另一种方式格式化其余单词。但我不知道如何实际做到这一点。
如果你想知道我到底为什么要这么做: 我想用它重新格式化古腾堡计划的整本书。单词应交替格式化,如我的旧帖子。长话短说:我希望通过这种方式来加速诱发字素联觉的进程。
答案1
此方法可以用于多个段落,并且总是以斜体开始新段落,然后与粗体交替。
\styleA
编辑以在用户可设置的宏和中定义两种样式\styleB
。
已重新编辑,因此\styleA
无需撤消\styleB
,反之亦然。
\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}
要使用接受参数的“样式”宏,上述答案的变体将是这样的:
\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}
答案2
纯 TeX 解决方案。感谢 Steven Segletes 向我展示如何将 catcode 分配包装在命令中。
\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
答案3
它可以处理段落并提供键值自定义。我的方法是基于三风格而不是两种:常见的在每个在里面和替代样式。这样可以将通用样式定义为空,这样更改是累积的,或者重置其间的所有内容。默认情况下,常见的重置字体样式和大小,在里面使用斜体和替代使用粗体。
这可以在全局和/或本地进行更改。
句法:
\astroalt[<key-value options>]{<text>}
\astroaltset{<key-value options>}
其中可用的键为:
init=<font switches>
alt=<font switches>
common=<font switches>
init
设置起始单词及其后每个单词的样式。alt
设置替代单词的样式。common
应用于init
或设置的样式之前的所有单词alt
。
用于\astroaltset{}
无限期地设置默认值。使用可选参数来\astroalt[]{}
设置一次性更改。
% 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}