[Tex/LaTex] Changing all “[” to \llbracket

stmaryrd

Is there a way to change all [ and ]s in a document to \llbracket and \rrbracket? Without modifying the text?

Best Answer

You can “easily” change [ and ] to mean \llbracket and \rrbracket in math mode:

\documentclass{article}

\usepackage{stmaryrd}

\begingroup
\catcode`[=\active \catcode`]=\active

% Define a meaning for active [ and ]
\gdef[{\llbracket} \gdef]{\rrbracket}

% Now take care of the \delcode of [ and ] for \left and \right
% First a temporary macro
\def\getdelim#1#2#3#4\relax{"#4}
% Use the temporary macro to get the right delcodes out of
% the meaning of \llbracket and \rrbracket
\global\delcode`[=\expandafter\getdelim\llbracket\relax
\global\delcode`]=\expandafter\getdelim\rrbracket\relax
\endgroup

% make [ and ] math active
\mathcode`\[="8000
\mathcode`\]="8000

\begin{document}
Here's a formula $a[b]=c[d]$; the brackets even change size with
\verb|\left| and \verb|\right|:
\[
\left[\frac{1}{2}\right]
\]
\end{document}

For text mode square brackets there is no way to do this automatically, because [ and ] has a syntactical meaning for many LaTeX commands.

enter image description here

Related Question