0

Is it possible to define a command that affects everything until the end of the current sentence?

For example, I am trying to define a command using a syntax similar to the following:

\documentclass{minimal}
\begin{document}

\sentencecommand This is the first sentence.
This is the second sentence.
This is the final sentence.

\end{document}

Where \sentencecommand affects only the sentence “This is the first sentence.”, but not any subsequent sentences?

1
  • 1
    as wipet shows you can use a . delimited argument but this fails if the sentence ends with ! for example, and in any case breaks all latex syntax guidelines. Far better to have \sentencecommand{This is the first sentence.} Feb 27, 2022 at 18:22

2 Answers 2

2

If the sentence is finished by period, then you can define, for example:

\def\sentencecommand #1.{{\it#1}.}

egreg shows "right tool" from his point of view (see comments), I can show "right tool" from my point of view: OpTeX's \eoldef and \replstring. You can see that the solution is much more complicated, there is no similar \def\foo#1.{} but we have to define three macros.

\eoldef\sentencecommand#1{\def\tmp{#1}%
   \replstring\tmp{.}{\use{.}}%
   \replstring\tmp{?}{\use{?}}\replstring\tmp{!}{\use{!}}%
   \ea\sentencecommandA\tmp\use{ }%
}
\def\sentencecommandA#1\use#2{{\it#1#2}}
\def\use#1{#1}

\sentencecommand This is the first sentence. This is the second sentence.
This is the final sentence.

\sentencecommand This is the first sentence? This is the second sentence.
This is the final sentence.

\sentencecommand This is the first sentence.
This is the second sentence.
This is the final sentence.

\sentencecommand This is the first sentence
and the text goes on.

\bye
3
  • would it be possible to check for a newline character too instead of a period ?
    – The Vekat
    Feb 27, 2022 at 18:24
  • 1
    newline character is transformed to space at tokenizer state (i.e. before macro expansion). Without very special settings is newline character invisible from macro point of view. But scanning a parameter of a macro to the end of line is possible, for example OpTeX provides \eoldef for such purposes. And scannig of the separator to the end of line OR period is much more complicated.
    – wipet
    Feb 27, 2022 at 19:19
  • Not really that complicated if you use the right tool, see my answer.
    – egreg
    Feb 27, 2022 at 22:44
1

You can look for an endline, a period, an exclamation mark or a question mark. Whichever comes first ends the processing and what's found is typeset in italics.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\sentencecommand}{}
 {
  \group_begin:
  \char_set_catcode:nn { "0D } { 12 }
  \peek_regex_replace_once:nn
   { (.*?) ([\x{0d}\.]) } % anything followed by ^^M or .
   { \c{textit} { \1 } \c{__thevekat_replace:n} { \2 } }
 }

\cs_new_protected:Nn \__thevekat_replace:n
 {
  \str_if_eq:nnTF { #1 } { . } { .\__thevekat_remove: } { ~ \group_end: }
 }
% we need to remove a possible \endlinechar (of category 12 after the period)
\cs_new_protected:Nn \__thevekat_remove:
 {
  \peek_charcode_remove:NTF \__thevekat_endline: { ~ \group_end: } { \group_end: }
 }
\use:x { \cs_set_eq:NN \exp_not:N \__thevekat_endline: \char_generate:nn { "0D } { 12 } }

\ExplSyntaxOff

\begin{document}

\sentencecommand This is the first sentence. This is the second sentence.
This is the final sentence.

\sentencecommand This is the first sentence.
This is the second sentence.
This is the final sentence.

\sentencecommand This is the first sentence
and the text goes on.

\end{document}

enter image description here

Not really that complicated: a group is opened and the \endlinechar is made category code 12, so we can look for it with a regular expression with \x{0D} (this assumes the standard value for \endlinechar). But the regular expression can also look for the first period, exclamation mark or question mark. What's been found is typeset in italics and the stopping character is processed: if it is the \endlinechar it is transformed into a space and the group is closed. Otherwise the character is typeset and an \endlinechar is looked for; if it is found it's transformed into a space. In either case the group is closed.

However, the syntax \sentencecommand{This is the first sentence.} is easier to maintain and less error prone.

You must log in to answer this question.

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