我想重新定义 & 符号以扩展为宏,我已定义。基本上它是/应该是这样的
&={\hspace*{30pt}Ampersand has been redefined\hfill}
最终用途是
\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}
顺便说一句:该\ns
命令完全按预期工作。
答案1
目前尚不清楚的预期用途是什么\ns
;不过,这里有一个实现。
我修复了你的代码中最明显的弱点。
\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}