[texhax] read argument until EOL

Heiko Oberdiek heiko.oberdiek at googlemail.com
Thu Jan 6 16:24:23 CET 2011


On Thu, Jan 06, 2011 at 11:24:22AM +0000, Philip Taylor (Webmaster, Ret'd) wrote:

> Arno Trautmann wrote:
> >Hi all,
> >
> >a question I posted on the LaTeX3-list and Uwe Lueck asked me to re-post
> >here:
> >
> >How to code a macro that reads an argument until the end of the line?
> >e.g. to get rid of braces in \section:
> >
> >   \eolsection My Title
> >   normal text.
> >
> >Here, "My Title" should be accesible in the \eolsection as #1.
> >For my case, the solution does not have to be robust or cover any
> >strange case with catcodes, verbatim or anything else, just plain text
> >will appear.
> 
> I am going to take my life in my hands and type in an extempore
> solution without testing : /caveat emptor/ !
> 
> \def \eolsection
> 	{
> 		\begingroup
> 		\catcode `\^^M = \active
> 		\uccode `\~ = `\^^M
> 		\uppercase {\def \innereolsection ##1~}{\endgroup \message {##1}}\relax
>         	\innereolsection
> 	}
> 
> Where I have "\message {##1}", you should insert whatever
> you want to do with My Title and analogous parameters.
> 
> THIS CODE IS UNTESTED : USE IS ENTIRELY AT YOUR OWN RISK.

"1" might have the wrong uccode different than 0 or `1.
(But I would consider it as quite small risk.)

Here is a different design (using some of the code above).
Macro \eolgrab executes it first argument with the
rest of the line as argument.

%%% test.tex %%% begin %%%
% preamble loads package ltxcmds for compatibility to plain TeX
% and sets the catcode of "@" to letter
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
  \input ltxcmds.sty\relax
\else
  \RequirePackage{ltxcmds}[2009/08/05]%
\fi
\edef\restoreatcode{\catcode64=\the\catcode64\relax}
\catcode64=11 %

% define macro \eolgrab
\def\eolgrab#1{%
  \begingroup
  \catcode`\^^M=\ltx at active
  \@eolgrab{#1}%
}
\begingroup
  \catcode`\^^M=\ltx at active%
\ltx at firstofone{\endgroup%
  \def\@eolgrab#1#2^^M{%
    \endgroup%
    #1{#2}%
  }%
}%

% postamble: restore previous catcode of "@"
\restoreatcode

% example: simple LaTeX document
\eolgrab\documentclass article
\eolgrab\begin document
\eolgrab\section Hello World
\eolgrab\end document
%%% test.tex %%% end %%%

Example for plain TeX:

\eolgrab\message Some text
\eolgrab{\immediate\write16}Hello World
\end

Also the definitions of \eol... macros are simplified:

\def\eolsection{\eolgrab\section}
% or in LaTeX
\newcommand*{\eolsection}{\eolgrab\section}

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list