Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This MWE is from a real case where an error occurs (with a hint about WSF from suffix package, but below the error is different) with \globaldefs=1\relax in a \begingroup \endgroup pair, in order to make \newcommand macros visible outside the pair.

This is main mwe.tex.

\documentclass[a4paper,10pt]{report}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{suffix,xargs}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Code borrowed from `multitoc.dtx' CTAN /macros/latex/contrib/ms %%
%%% Copyright [1998..1999]  Martin Schr\"oder. All rights reserved. %%
% \write with appropriate handling of |\protect| and |\thepage|; args:
% an output stream, some text to write, and some initialization code.
\newcommandx*\protected@write@immediate[3][3]{%
  \begingroup
    \let\thepage\relax
    #3%
    \let\protect\string
    \edef\reserved@a{\immediate\write#1{#2}}%
    \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi%
}
%%%                                                                 %%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommandx\MWE[7][3=mwe,4=-1,5=to,6=newcommand,7=12]{%
    \MWE*[#1][#2]%
    \catcode`\\=#7\catcode`\{=#7\catcode`\}=#7\catcode`\$=#7%
    \catcode`\#=#7\catcode`\^=#7\catcode`\_=#7\catcode`\%=#7%
    \let\@tempa\endlinechar%
    \endlinechar=#4%
    \immediate\read\my@file#5\my@lin@%
    \endlinechar=\@tempa%
    \catcode`\%=14\catcode`\_=8\catcode`\^=7\catcode`\#=6%
    \catcode`\$=3\catcode`\}=2\catcode`\{=1\catcode`\\=0%
    \protected@xdef\my@line{\my@lin@}%
    \protected@write@immediate\my@fil@{%%%%%%%%%%%%%%%%%%%%%
\@backslashchar#6\@backslashchar#3\@charlb\my@line\@charrb}%
    \MWE*%
    \input{#2}%
    \MWE*[#1]%
}

\WithSuffix\newcommandx\MWE*[3][1,2,3=.tex]{%
    \ifx\\#2#3\\%
        \immediate\closeout\my@fil@%
        \immediate\closein\my@file%%
    \else\ifx\\#2\\%
        \immediate\openin\my@file=#1#3%%
    \else%
        \newread\my@file%%
        \immediate\openin\my@file=#1#3%%
        \newwrite\my@fil@%
        \immediate\openout\my@fil@=#2#3%
    \fi\fi%
}

\makeatother

\begin{document}

    \begingroup\inputencoding{ascii}
        \globaldefs=1\relax
        \MWE{tst}{tmp}
    \endgroup

    \begingroup\inputencoding{ascii}
        \mwe
    \endgroup

\end{document}

The tst.tex contains just the "Hello world." line.

In the second \begingroup the control sequence \mwe is undefined. Were i to replace \MWE with this

\newcommandx\MWE[7][3=mwe,4=-1,5=to,6=newcommand,7=12]{%
    \newread\my@file%%
    \immediate\openin\my@file=#1.tex%%
    \newwrite\my@fil@%
    \immediate\openout\my@fil@=#2.tex%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \MWE*[#1][#2]%

    \catcode`\\=#7\catcode`\{=#7\catcode`\}=#7\catcode`\$=#7%
    \catcode`\#=#7\catcode`\^=#7\catcode`\_=#7\catcode`\%=#7%
    \let\@tempa\endlinechar%
    \endlinechar=#4%
    \immediate\read\my@file#5\my@lin@%
    \endlinechar=\@tempa%
    \catcode`\%=14\catcode`\_=8\catcode`\^=7\catcode`\#=6%
    \catcode`\$=3\catcode`\}=2\catcode`\{=1\catcode`\\=0%

    \protected@xdef\my@line{\my@lin@}%
    \protected@write@immediate\my@fil@{%%%%%%%%%%%%%%%%%%%%%
\@backslashchar#6\@backslashchar#3\@charlb\my@line\@charrb}%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%% \MWE*%
    \immediate\closeout\my@fil@%
    \immediate\closein\my@file%%
    \input{#2}%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \MWE*[#1]%
    \immediate\openin\my@file=#1.tex%
}

the MWE recognizes ok the \mwe command between the two \begingroup \endgroup pairs.

What i want is the same as in the following simplified MWE, but in a different setting; that is, to have a command \mwe available between \begingroup \endgroup pairs.

\documentclass[a4paper,10pt]{report}

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

\begin{document}

    \begingroup\inputencoding{ascii}
        \globaldefs=1\relax
        \newcommand\mwe{Hello world.}
    \endgroup

    \begingroup\inputencoding{ascii}
        \mwe
    \endgroup

\end{document}

Comment out \globaldefs=1\relax and \mwe is undefined in the second pair...

Appreciate your help, Thank s

Using \long\def is not working either.

\newcommandx\MWE[8][3=mwe,4=-1,5=to,6=long,7=12,8=gdef]{%
    \MWEZ[#1][#2]%
    \catcode`\\=#7\catcode`\{=#7\catcode`\}=#7\catcode`\$=#7%
    \catcode`\#=#7\catcode`\^=#7\catcode`\_=#7\catcode`\%=#7%
    \let\@tempa\endlinechar%
    \endlinechar=#4%
    \immediate\read\my@file#5\my@lin@%
    \endlinechar=\@tempa%
    \catcode`\%=14\catcode`\_=8\catcode`\^=7\catcode`\#=6%
    \catcode`\$=3\catcode`\}=2\catcode`\{=1\catcode`\\=0%
    \protected@xdef\my@line{\my@lin@}%
    \protected@write@immediate\my@fil@{%%%%%%%%%%%%%%%%%%%%%
\@backslashchar#6\@backslashchar#8\@backslashchar#3\@charlb\my@line\@charrb}%
    \MWEZ%
    \input{#2}%
    \MWEZ[#1]%
}

\newcommandx\MWEZ[3][1,2,3=.tex]{%
    \ifx\\#2#3\\%
        \immediate\closeout\my@fil@%
        \immediate\closein\my@file%%
    \else\ifx\\#2\\%
        \immediate\openin\my@file=#1#3%%
    \else%
        \newread\my@file%%
        \immediate\openin\my@file=#1#3%%
        \newwrite\my@fil@%
        \immediate\openout\my@fil@=#2#3%
    \fi\fi%
}
share|improve this question
1  
Are you really wanting to define \MWE in terms of itself? – egreg May 26 '12 at 19:53
i've already tested :) a variant just renamed \MWEZ without \WithSuffix for \MWE* so it's sure the error is different. – sjbiaga May 26 '12 at 19:57
1  
This is bound to fail in many ways. The \catcode assignments made with \globaldef=1 will survive the group, for example. It's quite difficult to understand what you want to do, in the first place. – egreg May 26 '12 at 20:02
They say positive \globaldefs in a group makes all macro definitions \global, as in the simplified MWE; i need \newcommand's between \b..g \e..g pairs. – sjbiaga May 26 '12 at 20:13
1  
Of course \long\gdef\mwe{Hello} would be completely equivalent and won't need any \globaldefs=1. So the example is too simplistic. – egreg May 26 '12 at 20:13
show 2 more comments

closed as not a real question by lockstep, Joseph Wright Jul 7 '12 at 21:53

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

Browse other questions tagged or ask your own question.