2

I'm trying to define a command that, given input on the form \mydef{pre}{main}{post}, gives output akin to:

1

(Where the "foo" and "bar" are just for reference, and the fraction inside is to provide a tall input.) That is, I want a paired delimiter with arguments for pre- and postscripts as subscripts. I want an unscaled default version, and preferably a starred version which scales automatically depending on main, but where the prescript is aligned with the postscript. The above is generated by the following:

\documentclass{article}

\usepackage{mathtools}

\DeclarePairedDelimiterXPP{\mydef}[3]{_{#1}}{[}{]}{_{#3}}{#2}

\begin{document}

\[
    foo \mydef*{pre}{\frac{main}{main}}{post} bar
\]

\end{document}

I figured that the \DeclarePairedDelimiterXPP command provided by mathtools was a good start, but I can't seem to get the prescript to align properly.

The \prescript command provided by the same package might be useful, but if you are going to recommend it, remember that it renders subscripts as if there were a superscript as well, which I don't want:

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\[
    {}_{pre} main \quad \prescript{}{pre}{main} \quad {}_{pre}^{} main
\]

\end{document}

3

So I would want a way around that.

Danny
  • 367
  • How you want the prescript to align is not clear. Could you give more details? – Bernard Nov 13 '17 at 19:34
  • @Bernard I want the prescript to align vertically with the postscript. That is, they should be the same "height", but otherwise it should appear exactly as above. – Danny Nov 13 '17 at 19:42
  • To be precise: The placement of the postscript above is good. It is the placement of the prescript that I am having trouble with. – Danny Nov 13 '17 at 20:37
  • 1
    Following https://tex.stackexchange.com/a/109695 you can use the tensor package, e.g., \newcommand{\mydef}[3]{\tensor*[_{#1}]{#2}{_{#3}}}. – Marijn Nov 13 '17 at 20:55
  • @Marijn That seems to do the job reasonably well. I'm still not convinced about the vertical placement, but that's beyond this particular question, I think. – Danny Nov 13 '17 at 21:13

2 Answers2

2

here's a rather sledgehammer-ish approach.

two methods are shown. the first -- the "obvious" approach -- doesn't work. i haven't figured out why.

the important point is the requirement to specify \displaystyle when constructing the box. i failed to apply that at first, and the pre-script was set too high. after i realized what the problem was, i tried fixing it in both places. unfortunately, the attempt to apply a phantom fails.

\documentclass{article}

\usepackage{mathtools}

\DeclarePairedDelimiter{\mydefa}{[}{]}

\newcommand{\mydef}[3]{%
  \vphantom{\displaystyle\mydefa*{#2}}_{#1}\mydefa*{#2}_{#3}}

\newcommand{\mydefb}[3]{%
  \setbox0=\hbox{$\displaystyle\mydefa*{#2}$}%
  \rule[-\dp0]{0pt}{\ht0} _{#1}\mydefa*{#2}_{#3}}

\begin{document}

trying to use a \verb+\vphantom+:
\[
    foo \mydef{pre}{\frac{main}{main}}{post} bar
\]

using the dimensions of the box created from the core element:
\[
    foo \mydefb{pre}{\frac{main}{main}}{post} bar
\]

\end{document}

output of example code

1

With \mydef we simply grab whatever happens to be up to the first brace, then pass control to \mydefA that grabs the real arguments:

\documentclass{article}
\usepackage{mathtools}

\newcommand{\mydef}{}% initialize
\def\mydef#1#{\mydefA{#1}}
\newcommand{\mydefA}[4]{%
  \mathopen{}\mathclose{\vphantom{\mydefB#1{#3}}}_{#2}\kern-\scriptspace
  \mydefB#1{#3}_{#4}%
}
\DeclarePairedDelimiter{\mydefB}{[}{]}

\begin{document}

\[
x \mydef*{pre}{\frac{main}{main}}{post} y \mydef[\Big]{pre}{\frac{main}{main}}{post} z
\]

\end{document}

Alternatively, and perhaps easier to understand,

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}

\NewDocumentCommand{\mydef}{sO{}mmm}{%
  \IfBooleanTF{#1}
   {\mathopen{}\mathclose{\vphantom{\mydefB*{#4}}}_{#3}\kern-\scriptspace\mydefB*{#4}_{#5}}%
   {\mathopen{}\mathclose{\vphantom{\mydefB[#2]{#4}}}_{#3}\kern-\scriptspace\mydefB[#2]{#4}_{#5}}%
}
\DeclarePairedDelimiter{\mydefB}{[}{]}

\begin{document}

\[
x \mydef*{pre}{\frac{main}{main}}{post} y \mydef[\Big]{pre}{\frac{main}{main}}{post} z
\]

\end{document}

enter image description here

egreg
  • 1,121,712