1

I'd like to have a command that defines an alias for an existing environment. My idea:

\newcommand{\envalias}[2]{
    \let\#1\#2
    \let\end#1\end#2
}

So e.g. \envalias{eq}{equation} would give me an environment eq that does exactly what equation does.

However, I can't figure out how to make use of \csname, \expandafter etc. to get this to work.

Ideally, \envalias could have an optional argument to decide wether an existing environment should be overwritten or not, but this.

I'm glad for any advise :)


Update

Thanks for the provided answers and the valuable feedback! I will think about whether I actually use this or not.

Just FYI: My particular use case actually is to ease collaboration: A collegue of mine as a different convention for naming thm envs and I didn't want to do lenghty copy-pasting to provide all theorems in both variants. However, theaching my editor to auto complete is worth a thought.

1
  • 3
    bad idea. Better use an editor that automatically inserts the environments
    – user187808
    Apr 30 '19 at 16:15
1

This does it.

\documentclass{article}
\newcommand{\envalias}[2]{\newenvironment{#1}{\begin{#2}}{\end{#2}}}
\begin{document}
\envalias{eq}{equation}
\begin{eq}
 E=mc^2
\end{eq}
\end{document}

However, I agree with jackfrost that this is not a good idea, to put it mildly. Let me try to talk you out of this. You probably won't exclude that you will at some point collaborate with others. I can tell you from my own experience that one is not thrilled if one gets to work on files from others where all sorts of obscure environments and macros are defined. Even better, different folks in the collaboration have mutually incompatible definitions.

6
  • 1
    I really wanted to know what "So ein Hirsch" (such a deer) idiomatically means. Apr 30 '19 at 16:31
  • 1
    @StevenB.Segletes The literal translation is more something like "Such a deer" and it means that the person referred to did not act as cleverly as one might have wished. My English is not good enough to provide an accurate translation, but it is definitely not as strong/insulting as "Such a fool" or "Such a clown" but goes a bit in this direction, but I removed it anyway because it is not 100% polite either.
    – user121799
    Apr 30 '19 at 16:36
  • @UlrichDiez Sure, this is why I added this in the first place, but I also removed it because this is not the way we should interact here, I think.
    – user121799
    Apr 30 '19 at 16:48
  • 1
    Here is one example of where this approach won't work (in favour of avoiding such shorthand).
    – Werner
    Apr 30 '19 at 17:19
  • @Werner Thanks! Yes, I know that there are quite a few environments which want to see an explicit \end{whatever}, and I agree with you that this adds to the arguments against introducing these shorthands. Here and here are more examples of this type.
    – user121799
    Apr 30 '19 at 17:21
0

Do this only with environments which neither do check whether \@currenvir yields a specific string nor do search for the sequence \end{<environment>}, which ends the environment, literally - the latter is the case, e.g., with the verbatim-environment:

\documentclass{article}
\usepackage{amssymb} %<- only needed for the nice \mathbb-command below. ;-)

\makeatletter
\newcommand\bracestripexchange[2]{#2#1}%
\@ifdefinable{\name}{\long\def\name#1#{\romannumeral0\innername{#1}}}%
\newcommand\innername[2]{%
  \expandafter\bracestripexchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\newcommand{\envalias}[2]{%
  \name\@ifdefinable{#1}{%
    \name\name\let{#1}={#2}%
    \name\name\let{end#1}={end#2}%
  }%
}%
\makeatother

\envalias{eq}{equation}

\begin{document}

\begin{equation}
\{x\in\mathbb{N}\}\land\{y\in\mathbb{N}\}\land\{z\in\mathbb{N}\}\to x^3 + y^3 \neq z^3
\end{equation}

\begin{eq}
\{x\in\mathbb{N}\}\land\{y\in\mathbb{N}\}\land\{z\in\mathbb{N}\}\to x^4 + y^4 \neq z^4
\end{eq}

\end{document}

What is meant by my remark about checks for \@currenvir yielding specific strings is shown here:

\documentclass{article}
\usepackage{amssymb} %<- only needed for the nice \mathbb-command below. ;-)

\makeatletter
\newcommand\bracestripexchange[2]{#2#1}%
\@ifdefinable{\name}{\long\def\name#1#{\romannumeral0\innername{#1}}}%
\newcommand\innername[2]{%
  \expandafter\bracestripexchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\newcommand{\envalias}[2]{%
  \name\@ifdefinable{#1}{%
    \name\name\let{#1}={#2}%
    \name\name\let{end#1}={end#2}%
  }%
}%

\def\someenvironmentstring{someenvironment}
\newenvironment{someenvironment}
               {\ifx\someenvironmentstring\@currenvir Case A\else Case B\fi:}
               {}

\makeatother

\envalias{otherenvironment}{someenvironment}

\begin{document}

\begin{someenvironment}
Body of Environment
\end{someenvironment}

\begin{otherenvironment}
Body of Environment
\end{otherenvironment}

\end{document}
0

Not a good idea. Anyway, if you like to live dangerously,

\newcommand{\envalias}[2]{%
  \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname
  \expandafter\let\csname end#1\expandafter\endcsname\csname end#2\endcsname
}

but beware that often environments are defined in terms of other ones. For instance, \endquote is defined as \endlist and if you do \renewenvironment{list} (just to make a simple example), you'll end up screwing your environment aliased to quote.

You gain very little in doing \envalias{eq}{equation}: you just save some keys that you could instruct your editor to insert instead of typing them yourself. The only gain I see is in obfuscation of your code.

3
  • I was not aware that something like \newcommand{envalias}... works, I always thought there should be a backslash, \newcommand{\envalias}.... So this produces an even more interesting result.
    – user121799
    May 1 '19 at 14:05
  • @marmot That was a typo, which you could have fixed straight away.
    – egreg
    May 1 '19 at 18:23
  • Well, you point out that in my answer something produces an interesting result even though I do not make any statement on that scenario, and even though I try to discourage the user from doing these things. And your actual proposal did not work. Why is your comment on my answer OK and mine not?
    – user121799
    May 1 '19 at 18:28

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.