I want to define a macro that shows an array access in a language like in C or Java.
My first try looked like this:
\newcommand{\accessI}[2]{#1\left[#2\right]}
\[
\accessI{a}{i}
\]
I didn't like this because the kerning of the brackets didn't seem right (they should be closer, imho). The kerning can be easily fixed with \mkern.
\newcommand{\accessII}[2]{#1\mkern-3mu\left[#2\right]}
\[
\accessII{a}{i}
\]
This looks great, but when I use terms like this in a subscript, the kerning looks bad again (this time the brackets are too close).
\[
A_{\accessII{a}{i}}
\]
I'm now looking for either
- a command like
\mkernthat is context-dependent (i.e. that shrinks the adjustments when used in a subscript) or - a way to tell in which context the macro is used so that I can use different
\mkern-calls in the\accessIImacro





\mleftand\mrightmacros? – Mico Aug 25 '22 at 19:53$$in a LaTeX document to initiate and terminate display math mode; instead, do use\[and\]. For a longer discussion of this subject, please see the posting Why is\[ ... \]preferable to$$ ... $$? – Mico Aug 25 '22 at 20:29