0

I want to redefine the ampersand-charakter to expand to a macro, I defined. Basically it is/should be something like

&={\hspace*{30pt}Ampersand has been redefined\hfill}

The final use is

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{environ}
\usepackage{etoolbox}
\usepackage{xparse}

\newcommand{encap}[1]{ { % make definitions of \ns local / limit scope
\DeclareDocumentCommand{\ns}{s O{0 pt}}{%
    \ifx\tempi\undefined%
        \newlength{\tempi}%
    \fi%
    \setlength{\tempi}{##2}%
    \divide\tempi by 2%
    \ifx##1\BooleanTrue \hskip 0pt plus 1fill minus 1fill \hspace*{\tempi}-\hspace*{\tempi}%
        \hskip 0pt plus 1fill minus 1fill%
    \else% 
        \hskip 0pt plus 2fill minus 2fill \hspace*{##2}%
    \fi%
}%\ns

%Pseudocode:
&=\ns

\makebox[\textwidth]{#1}
}} % encap

\begin{document}
\encap{this\ns equals& this}
\ns % out of scope
& % should be restored
\end{document}

BTW.: The \ns command works exactly as intended

  • 1
    Why don't you make a complete example? And did you really thought throught the consequences if you make & is active and it no longer can be used in a tabular? – Ulrike Fischer May 27 '16 at 15:01
  • @UlrikeFischer I thought this through (at least I think). Inside of \encap there can't be used the tabular-environment, but this is absolutely okay. – MaestroGlanz May 27 '16 at 15:11
  • So you want that & does \ns only inside the argument to \encap? – egreg May 27 '16 at 15:16
  • 1
    You could use expl3 and do a \tl_replace_all:Nnn \l_tmpa_tl { & } { \ns }. – Manuel May 27 '16 at 15:27
  • 1
    @MaestroGlanz It won't break. – Manuel May 27 '16 at 15:38
0

It's not clear what is the intended usage of \ns; however, here's an implementation.

I fixed the most obvious weaknesses in your code.

\documentclass[12pt,a4paper]{article}

\usepackage{xparse}

\newlength{\tempi}

% define active & to be normal &
\begingroup\lccode`~=`&
\lowercase{\endgroup\let~}&
% make & active
\catcode`&=\active

\newcommand{\encap}[1]{\begingroup\let&\ns#1\endgroup}

\NewDocumentCommand{\ns}{s O{0 pt}}{%
  \unskip
  \setlength{\tempi}{\dimexpr#2/2}%
  \IfBooleanTF{#1}
    {%
     \hspace{0pt plus 1fill minus 1fill}%
     \hspace*{\tempi}-\hspace*{\tempi}%
     \hspace{0pt plus 1fill minus 1fill}%
    }
    {%
     \hspace{0pt plus 2fill minus 2fill}\hspace*{#2}%
    }%
  \ignorespaces
}

\begin{document}

\noindent
\makebox[\textwidth]{\encap{this \ns equals & this}}

\noindent
\makebox[\textwidth]{\encap{this \ns* equals &* this}}

\noindent
\makebox[\textwidth]{\encap{this \ns*[10pt] equals &*[10pt] this}}

\begin{tabular}{cc}
 text & text \\
 a & b
\end{tabular}

\end{document}

enter image description here

  • That's ecactly what it should do. – MaestroGlanz May 27 '16 at 15:36

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.