13

I am trying to typeset a "fraction" without the fraction bar. I have tried to do this using the genfrac, but as in the example below, the numerator and denominator in genfrac are farther apart than in frac. How can I correct this?

I'm trying to create a table in which one of the entries ("cells") contains two entries -- one on top of the other -- as in row 5 in the MWE below. In other words, I want to make the fraction bar "invisible" in the MWE table below.

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[T1]{fontenc}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
\usepackage{booktabs}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage[subrefformat=parens]{subcaption}

\begin{document}

$\frac{1}{2}\;\genfrac{}{}{0pt}{}{1}{2}$

\begin{table}[!h]
  \begin{tabular}{lll}
    Row 1 & 1.23 & Some text.\\
    Row 2 & 7.89 & Additional text.\\
    Row 3 & 4.56 & More text.\\
    \addlinespace
    Row 4 & 1.23 & Some text.\\
    Row 5 & $\frac{\textit{A}:\;3.14}{\textit{B}:\;6.28}$ & Additional text.\\
    Row 6 & 4.56 & More text.\\
  \end{tabular}
\end{table}

\end{document}

examples

1
  • 1
    Just to make sure, you are aware that the usual approach is a row spanning multirow? But this makes "Row 5" and "Additional text." twice as tall instead of the other entry half-sized.
    – Teepeemm
    Commented Mar 26, 2021 at 21:16

4 Answers 4

8

That's a bit unexpected, I'll have to read appendix G again... but \genfrac is just exposing the underlying primitive and \atop and \above0pt both omit the rule but increase the height of the numerator. You can specify the negative width of the default rule (-0.4pt) to get the same height as \frac (which uses the \over primitive).

The first four forms here use the same height as\frac, the last three are higher.

enter image description here

Note this only applies to textstyle not displaystyle.

\documentclass[oneside,10pt]{book}
\usepackage{amsmath}

\usepackage[T1]{fontenc}

\begin{document}

\hrule
$
\frac{1}{2}\;
\genfrac{}{}{-.4pt}{}{1}{2}\;
{1 \above -.4pt 2}\;
{1\over 2}\;
{1 \above 0pt 2}\;
\genfrac{}{}{0pt}{}{1}{2}\;
{1\atop 2}
$
\hrule
\smallskip
\hrule
$\displaystyle
\frac{1}{2}\;
\genfrac{}{}{-.4pt}{}{1}{2}\;
{1 \above -.4pt 2}\;
{1\over 2}\;
{1 \above 0pt 2}\;
\genfrac{}{}{0pt}{}{1}{2}\;
{1\atop 2}
$
\hrule


\end{document}
8

The \frac macro is defined as \begingroup #1\endgroup \over #2 (for the definition enter texdef -t latex \frac in a terminal). You can redefine this to print the \over line in white:

\newcommand{\fracnoline}[2]{{\begingroup #1\endgroup \color{white}\over\color{black} #2}}

As Mico noted in comments, to generalize the macro in case the original color is not black you can use the following definition:

\newcommand{\fracnoline}[2]{\colorlet{foo}{.}{\begingroup #1\endgroup \color{white}\over\color{foo} #2}}

Full code:

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[T1]{fontenc}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
\usepackage{booktabs}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage[subrefformat=parens]{subcaption}
\usepackage{xcolor}

\begin{document}
\newcommand{\fracnoline}[2]{\colorlet{foo}{.}{\begingroup #1\endgroup \color{white}\over\color{foo} #2}}

$\frac{1}{2}\;\genfrac{}{}{0pt}{}{1}{2}$

\begin{table}[!h]
  \begin{tabular}{lll}
    Row 1 & 1.23 & Some text.\\
    Row 2 & 7.89 & Additional text.\\
    Row 3 & 4.56 & More text.\\
    \addlinespace
    Row 4 & 1.23 & Some text.\\
    Row 5 & $\frac{\textit{A}:\;3.14}{\textit{B}:\;6.28} \fracnoline{\textit{A}:\;3.14}{\textit{B}:\;6.28}$ & Additional text.\\
    Row 6 & 4.56 & More text.\\
  \end{tabular}
\end{table}

\end{document}

Result:

enter image description here

2
  • 3
    +1. Just in case the prevailing color isn't black, you may want to change \newcommand{\fracnoline}[2]{{\begingroup #1\endgroup \color{white}\over\color{black} #2}} to \newcommand{\fracnoline}[2]{\colorlet{foo}{.}{\begingroup #1\endgroup \color{white}\over\color{foo} #2}}. (The dot in \colorlet{foo}{.} picks up the prevailing color.)
    – Mico
    Commented Mar 26, 2021 at 22:36
  • 1
    @Mico thanks for your comment, I added it to the answer.
    – Marijn
    Commented Mar 27, 2021 at 12:22
7

With amsmath you can use the \substack command:

\documentclass[oneside,11pt]{book}
\usepackage{amsmath}
\usepackage{booktabs}

\begin{document}
 
  \begin{table}[!h]
   \begin{tabular}{lll}
    Row 1 & 1.23 & Some text.\\
    Row 2 & 7.89 & Additional text.\\
    Row 3 & 4.56 & More text.\\
    \addlinespace
    Row 4 & 1.23 & Some text.\\
    Row 5 & $ \substack{\textit{A}:\;3.14\\[1pt]\textit{B}:\;6.28}$ & Additional text.\\
    Row 6 & 4.56 & More text.\\
  \end{tabular}
 \end{table}
 
\end{document}

enter image description here

2
  • 1
    The correct answer IMO, don't abuse a fraction to do this.
    – BrtH
    Commented Mar 27, 2021 at 16:55
  • If you post that as an asnwer I can upvote it. I do agree. For tables, use table tools. Commented Mar 30, 2021 at 23:51
1

Starting from David's answer, a fixed version that works in display, text and script style (not at scriptscript style, where other optimizations are used).

\documentclass[oneside,10pt]{book}
\usepackage{amsmath}
\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[T1]{fontenc}

\makeatletter
\DeclareRobustCommand{\xabove}[2]{{%
  \mathpalette\fix@genfrac{{#1}{#2}}%
}}
\newcommand{\fix@genfrac}[2]{\fix@@genfrac#1#2}
\newcommand{\fix@@genfrac}[3]{%
  \genfrac{}{}{\fix@@@genfrac{#1}}{}{#2}{#3}%
}
\newcommand{\fix@@@genfrac}[1]{%
  \ifx#1\displaystyle 0pt\else
  \ifx#1\textstyle -\fontdimen8\textfont2 \else
  \ifx#1\scriptstyle -\fontdimen8\scriptfont2 \else
  0pt % but doesn't really work :-(
  \fi\fi\fi
}
\makeatother

\begin{document}

$\displaystyle\frac{1}{2}\xabove{1}{2}$

$\textstyle\frac{1}{2}\xabove{1}{2}$

$\scriptstyle\frac{1}{2}\xabove{1}{2}$

%$\scriptscriptstyle\frac{1}{2}\xabove{1}{2}$

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .