4

Out of curiosity, I wonder why this fails:

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\bla}{mm}{#1.#2!}

\begin{document}

\begingroup
\obeylines
\everypar={\bla}
{C}{G} skdjfs;ldjkf;sjkf
{ERT}{ERTERT} djkfsjdfl;ksjdf;l
\endgroup

\end{document}

with the error: Argument of \bla has an extra }. I did look at \par in the parameter text (as an argument delimiter) and \vbox{#1} in an \everypar macro but these are not delimited arguments to a macro.

Later...

I discovered that xparse offers a solution of sorts, though I think this might be looked upon as abuse:

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\bla}{O{}O{}}{#1.#2!}

\begin{document}

\begingroup
\obeylines
\everypar={\bla}
[C][Gin] skdjfs;ldjkf;sjkf
[ERT][ERTERT] djkfsjdfl;ksjdf;l
\endgroup

\end{document}

I assume that this works because the pesky { is no longer in play. This is convenient in that I can avoid putting \bla in front of each and every line (dozens of them).

2
  • Is there a reason you want \obeylines here? It makes the explanation longer ... though fundamentally the same
    – Joseph Wright
    Oct 4, 2018 at 12:11
  • No. It was part of an earlier experiment...
    – sgmoye
    Oct 4, 2018 at 12:15

1 Answer 1

5

TeX is in vertical mode when it reads your {C} start-of-paragraph text. The { starts a group but does not change mode. That happens when TeX reads C, which must be in horizontal mode and so starts a paragraph. The \everypar tokens are inserted, and your macro grabs C as #1 (you can see this if you use a macro with only one argument). In the current case, you then have a } in the input stream. That can't be #2, so you get a TeX error.

Probably the easiest way to avoid this is to force TeX to leave vertical mode. The standard \leavevmode won't work as it inserts some tokens that come after \everypar. We can fix that with an e-TeX definition for \leavevmode

\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}

which can then be inserted

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\bla}{mm}{#1.#2!}
\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}

\begin{document}

\begingroup
\everypar={\bla}
\leavevmode
{C}{G} skdjfs;ldjkf;sjkf
{ERT}{ERTERT} djkfsjdfl;ksjdf;l
\endgroup

\end{document}
7
  • 1
    What is e-tex specific here? The \protected naturally, but it seems to work also without it. Oct 4, 2018 at 12:44
  • Somewhere in the TeXBook there is a warning about beginning a paragraph with { -- can't find it now, but that should have made me think... This was a nice lesson.
    – sgmoye
    Oct 4, 2018 at 12:46
  • Actually, having TeXed the code, I find that does not work. The first line works correctly giving typeset C.G! skdjfs;ldjkf;sjkf but the second line gives typeset E.R!ERTERT djkfsjdfl;ksjdf;l. The code works if \leavevmode is placed before each line.
    – sgmoye
    Oct 4, 2018 at 13:32
  • @sgmoye I've dropped the \obeylines as it makes life more tricky: perhaps you haven't?
    – Joseph Wright
    Oct 4, 2018 at 14:52
  • @JosephWright I had absently put a return after the fist line. That led me to the discovery that \leavevmode is required before each line, and the realization that there is no real labor-savings in using a macro with parameters inside \everypar It would be nice if there were...
    – sgmoye
    Oct 4, 2018 at 16:11

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.