2

I can't understand the way babel works (or, more precisely, doesn't) in the following example. The command \sujet is supposed to create a new command named \sujeti, which in turn is supposed to print the 4th argument of \sujet.

This seems to be quite the case except that 1) the punctuation, which should be French thanks to babel, is wrong (no space before '!'); 2) there is no way to use the \verb command in the content of \sujet.

Here is a minimal example:

\documentclass{article}

\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}

\RequirePackage[french]{babel}

\newcommand\addindex[4][]{% #2 as name of var, #3 as index and #4 as content
  \csname#1def\expandafter\endcsname\csname #2\roman{#3}\endcsname{#4}%
}

\newcounter{num_sujet}

\newcommand{\sujet}[1]{%
    \stepcounter{num_sujet}
    \addindex{sujet}{num_sujet}{#1\par} 
    \par   
}

\sujet{%
    Test! %\verb/there should be a space before '!', and \verb doesn't seem to work/
}

\begin{document}
    \sujeti
\end{document}

Any help or explanation on what is going on would be very welcome.

1 Answers1

4

Package babel does not make its shorthand active in the preamble to prevent that the loading of other packages is disturbed. The macros \shorthandon and shorthandoff can activate and deactivate the shorthand for the text in the preamble. The following example simplifies the original MWE to show the point more clearly:

\documentclass{article}

\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}

\RequirePackage[french]{babel}

\newcommand{\sujet}[1]{%
    \def\sujeti{#1}%
}

\shorthandon{!}
\sujet{Test!}
\shorthandoff{!}

\begin{document}
    \sujeti
\end{document}

Result

Heiko Oberdiek
  • 271,626