[texhax] Copying xparse macros and environments

Brian Dunn bd at bdtechconcepts.com
Wed May 30 10:37:43 CEST 2018


Hi all,

There are arguments against adding a way to \let an xparse command:
https://tex.stackexchange.com/questions/285551/how-to-let-an-xparse-defined-command

In particular, the internals may be changed at some point.  But if the
xparse team were the ones to provide a hypothetical \LetDocumentCommand,
couldn't this command adapt to any changes?

Also, I came up with a way to copy an xparse command or environment with new
names.  Any arguments against \let may also apply to creating a copy, so
perhaps doing as in the following example might not be a good idea.
Thoughts?

(As far as "why?", I wish to copy the original to a new name, also
provide an alternative with yet another name, then redefine the original to
call either the copy of the original or the alternative, depending on a
current state. Such fun.)

---
\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}


\ExplSyntaxOn
% \copyxparse{originalname}{copyname}
\newcommand*{\copyxparse}[2]{
\expandafter\cs_new_eq:NN
    \csname#2\expandafter\endcsname
    \csname#1\endcsname
\expandafter\cs_new_eq:NN
    \csname #2 code\expandafter\endcsname
    \csname #1 code\endcsname
}
\ExplSyntaxOff


\ExplSyntaxOn
% \copyxparseenv{originalname}{copyname}
\newcommand*{\copyxparseenv}[2]{
\expandafter\cs_new_eq:NN
    \csname#2\expandafter\endcsname
    \csname#1\endcsname
\expandafter\cs_new_eq:NN
    \csname #2 code\expandafter\endcsname
    \csname #1 code\endcsname
\expandafter\cs_new_eq:NN
    \csname end#2\expandafter\endcsname
    \csname end#1\endcsname
\expandafter\cs_new_eq:NN
    \csname end#2 code\expandafter\endcsname
    \csname end#1 code\endcsname
}
\ExplSyntaxOff


\NewDocumentCommand{\origmacro}{m o}{%
Results: #1 #2
}

\copyxparse{origmacro}{copymacro}


\NewDocumentEnvironment{origenv}{m o}
{\par Environment results: #1 #2\par}
{\par endEnvironment results: #1 #2\par}

\copyxparseenv{origenv}{copyenv}


\begin{document}

\origmacro{first}

\origmacro{first}[second]

\copymacro{copied first}

\copymacro{copied first}[copied second]

\begin{origenv}{third}
Inside the original environment.
\end{origenv}

\begin{origenv}{third}[fourth]
Inside the original environment.
\end{origenv}

\begin{copyenv}{copied third}
Inside the copied environment.
\end{copyenv}

\begin{copyenv}{copied third}[copied fourth]
Inside the copied environment.
\end{copyenv}

\end{document}
---

-- 
Brian Dunn
BD Tech Concepts LLC
http://www.BDTechConcepts.com
bd at BDTechConcepts.com

http://www.linkedin.com/in/bdtechconcepts/


More information about the texhax mailing list