4

I am trying to put a fraction in quotes in LaTeX. I have the following so far:

\documentclass{article}

\newcommand*{\mquote}[1]{\text{``\(#1\)''}}

\begin{document}
\[
  \frac{f(x)}{g(x)} \implies \mquote{\frac{0}{0}}
\]
\end{document}

Unfortunately this puts the quotes too low. How can I get them to the top of the fraction?

2 Answers2

5

Measure the object to be quoted; if it's higher than the quotes, raise them.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\mquote}[1]{{\mathpalette\mqu@te{#1}}}

\newcommand{\mqu@te}[2]{%
  \sbox0{$\m@th#1\text{``}$}%
  \sbox2{$\m@th#1\text{''}$}%
  \sbox4{$\m@th#1#2$}%
  \ifdim\ht4>\dimexpr\ht0+1pt\relax
    \raisebox{\dimexpr\ht4-\height}{\box0}%
    #2%
    \raisebox{\dimexpr\ht4-\height}{\box2}%
  \else
    \box0 #2\box2
  \fi
}
\makeatother

\begin{document}
\[
  \mquote{x} \implies \mquote{\frac{0}{0}} \implies \mquote{A}
  \implies \mquote{\frac{\dfrac{x}{x+1}}{\dfrac{y^2}{y+1}}}
\]
\[
\textstyle \mquote{\frac{0}{0}}
\]
\end{document}

enter image description here

Some explanations. First, the usage of \mathpalette is explained in the answers to The mysteries of \mathpalette so I won't touch that topic; suffice it to say that in the code of \mqu@te, #1 stands for the current style. We need it because math is typeset differently in \displaystyle or in \textstyle. In the same macro, #2 stands for the subformula to be quoted.

The working is easy to explain. I set the quotes in two boxes, using the correct font size corresponding to the math style using \text. In the temporary box register 4 I set the formula to be quoted, so that we can access to its dimensions. Then the code checks whether the height of the formula is more than the height of the quotes (plus a small buffer of 1pt) and, if it is, raises the boxes containing the quotes by the height of the subformula diminished by the height of the quotes (\ht4-\height); in between, the formula is typeset. Otherwise, the boxes containing the quotes are delivered with the subformula in between them.

egreg
  • 1,121,712
  • Thanks so much. Could you explain a little more how this works? I've been trying to understand it and I get most of it, but its always nice to learn how to do things on my own :) – carloabelli Feb 03 '16 at 18:09
  • @cabellicar123 I added some explanations. Have fun! – egreg Feb 03 '16 at 18:24
  • Thanks! I find I learn the most when I get answers full of things I don't understand upon first glance – carloabelli Feb 03 '16 at 18:25
  • @cabellicar123 Since I'm a teacher, I like to teach people who want to learn! – egreg Feb 03 '16 at 18:26
  • One question. Why does replacing \height with \ht0 in \raisebox not work? – carloabelli Feb 03 '16 at 18:33
  • @cabellicar123 Good question! Because \raisebox typesets the material in order to set \height, \width and \depth, so after this the box is empty. I could have used \copy0 instead of \box0 (which wouldn't clear the box register), but since LaTeX provides \height, it's not necessary. – egreg Feb 03 '16 at 18:35
1

Does this meet your needs?

\documentclass{article}
\usepackage{amsmath,scalerel}
\newsavebox\tmpbox
\newcommand*{\mquote}[1]{\ThisStyle{\savebox{\tmpbox}{\hbox{$\SavedStyle#1$}}%
  \raisebox{\dimexpr\ht\tmpbox-.7\ht\strutbox}{``}\usebox{\tmpbox}%
  \raisebox{\dimexpr\ht\tmpbox-.7\ht\strutbox}{''}}}

\begin{document}
\[
  \frac{f(x)}{g(x)} \implies \mquote{\frac{0}{0}} \implies \mquote{A} 
  \implies \mquote{\frac{~\dfrac{x}{x+1}~}{\dfrac{y^2}{y+1}}}
\]
\end{document}

enter image description here