TransWikia.com

Environment (or similar) which replaces symbol with command

TeX - LaTeX Asked by Ryan Greenblatt on December 12, 2020

See this for an example of how replacing * with cdot would work.

I want to do the same type of thing but only within an environment. For example:

newenvironment{starmultiply}
{
% Something here...
}
{
% Undo previous effect
}

Another good usage would be to replace < and > with langle and rangle.

I don’t always want to do this throughout the document, so DeclareMathSymbol can’t be used (it is preamble only). Also,

newenvironment{starmultiply}
{
mathcode`*="8000 %
{catcode`*=activegdef*{cdot}}
}
{
% Undo previous effect
}

Doesn’t seem to work. I get "Undefined control sequence" errors. Is there any solution which isn’t super gross?

One Answer

You can simply change the math code of *. Doing gdef* is not recommended and wouldn't work like that anyway; there are better methods to do the definition only locally. See the extended code below.

documentclass{article}
usepackage{amsmath}

newenvironment{starmultiply}
 {mathcode`*=cdotignorespaces}
 {ignorespacesafterend}

begin{document}

This is a standard multiplication $acdot b$

begin{starmultiply}
This is a nonstandard multiplication $a*b$
end{starmultiply}

end{document}

enter image description here

For langle and rangle it's a bit more complicated:

documentclass{article}
usepackage{amsmath}

newenvironment{starmultiply}
 {%
  mathcode`*=cdot
  mathcode`<="8000
  mathcode`>="8000
  begingrouplccode`~=`< lowercase{endgrouplet~}langle
  begingrouplccode`~=`> lowercase{endgrouplet~}rangle
  ignorespaces}
 {ignorespacesafterend}

begin{document}

This is a standard multiplication $langle acdot brangle$

begin{starmultiply}
This is a nonstandard multiplication $<a*b>$
end{starmultiply}

end{document}

enter image description here

Correct answer by egreg on December 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP