# Math mode inside LyX tables

If I insert a math mode environment inside a table cell, then the output will show no vertical space between the borders and the equation, here is an example:

Is there any way that will make the borders have more vertical space from the formula?

• If you insert a simple float table, you can see the settings menu via: R-click on table -> Other... -> Settings -> Borders. Here you can add or subtract vertical space below, above the row. The best typographical advice is to switch the formal border setting, this will automatically set both the looking and the spacing of the entire table. – TheVal Nov 19 '13 at 14:10
• @Andrea L. Shouldn't this be posted as an answer? – landroni Jan 14 '14 at 6:59
• @landroni For the sake of coherence, yes. The moment I'll be able to typeset it nicely it will be posted (this delay is due to my first year as a freshmen, so I didn't have too much time)...;) – TheVal Jan 14 '14 at 18:10
• @AndreaL. Would you have time to write an answer now? – Torbjørn T. Mar 2 '14 at 9:50
• @TorbjørnT. I'd like to, and I'm terribly sorry for my delay (holy smoke, almost four months of waiting!); I think Yoav Fridman deserves more than a coffee for the patience. – TheVal Mar 4 '14 at 14:31

A rather simple workaround can be constructed using plain TeX/LaTeX boxes handling; since LaTeX's cell vertical space takes no account for the command \displaystyle, one solution is to avoid it "a priori" by inserting the equation inside a parbox environment:

\parbox[⟨alignment⟩][⟨height⟩][⟨arrangement⟩]{⟨width⟩}{⟨contents⟩}


Firstly, the alignment and arrangement parameters are set centered (c); the interesting dimensional arguments are height and width: since the equation will have stored its specific height (above the baseline), depth (below the baseline) and width, these dimensions could be easily substituted inside parbox's optional/mandatory arguments using calc's commands:

total height: \totalheightof{⟨content⟩}+2\fboxsep+2\fboxrule  (pt)
total width : \widthof{⟨content⟩}                             (pt)


noticing that this setting can be applied to both any text or math inside the box, a useful macro \mytabspace{⟨your equation⟩} could be created, defined by:

% implementing more dimension/expression handling control sequences
\usepackage{calc}
%
\newcommand\mytabspace[1]{%
\parbox[c][\totalheightof{#1}+2\fboxsep+2\fboxrule][c]{\widthof{#1}}{#1}%
}


Another equivalent solution would be storing the content of the sub-cell inside a temporary box \@tempboxa; therefore there would be no worries about repetitive implementation inside the document, because if the box dimensions are defined inside a group, once outside the command they reset to the new box contents:

\makeatletter
\newcommand\mytabspace[1]{%
\sbox\@tempboxa{#1}%
\parbox[c][\ht\@tempboxa+\dp\@tempboxa+2\fboxsep+2\fboxrule][c]{\wd\@tempboxa}{%
\usebox\@tempboxa%
}%
}
\makeatother


Seeing it in action with a simple MWE paids off all the work behind:

\documentclass{article}
%
\usepackage{calc}
%
\newcommand\mytabspace[1]{%
\parbox[c][\totalheightof{#1}+2\fboxsep+2\fboxrule][c]{\widthof{#1}}{#1}%
}
% comment out the lines below and comment the beforementioned command,
% you'll see that the output is the same.
%\makeatletter
%\newcommand\mytabspace[1]{%
% \sbox\@tempboxa{#1}%
% \parbox[c][\ht\@tempboxa+\dp\@tempboxa+2\fboxsep+2\fboxrule][c]{\wd\@tempboxa}{%
%  \usebox\@tempboxa%
% }%
%}
%\makeatother
%
\begin{document}
%
\begin{table}[ht]
\centering
\begin{tabular}{|c|c|}
\hline
Function   & Integral   \\
\hline
$-$  & This is not my macro: a   \\
\hline
% test if there are unwanted vertical space additions
$-$  & This is my macro: \mytabspace{a}  \\
\hline
% now the integral becomes:
$\sin(x)$  & \mytabspace{$\displaystyle{\lim_{n\to+\infty}\sum_{k=1}^n\sin(c_k)\Delta x_k=\int_a^b\sin(x)\,\mathrm{d}x}$}   \\
\hline
\end{tabular}
\caption{This is a spaced equation, finally at last!}
\end{table}
%
\end{document}


With the output shown, it can be observed that mytabspace applies no modifications to any vertical spacing, as a demonstration to its versatility:

Moving on with LyX, accessing the menu Document ￫ Settings ￫ LaTeX preamble any \mytabspace macro definition shown can be pasted; because the program itself adds automatically \makeatletter and \makeatother inside the white preamble (the second code should not be pasted with those two commands, they'll be redundant).

At this point, the desired math environment can be delimited by a simple ERT:

Giving the final, expected output:

In addition, I'd like to thank @DavidCarlisle for the useful tips (but, only for the second code =P); and more importantly:

I too encountered this issue when I was just getting started with LyX (as example, I didn't even know what did "control sequence" mean back then); but I've been able to face it just now.

• each cell is a group and you save and use the box in the same cell so so you do not need to allocate any boxes or lengths you can use the same box (eg box0 or @tempboxa) for every cell. As a first approximation you should never use \newbox or \newlength inside anothe command. – David Carlisle Mar 11 '14 at 10:46
• @DavidCarlisle I still remember your comments from yesterday's chat, and right now I'm testing the results. A new edit will come soon, probably before tonight. – TheVal Mar 11 '14 at 14:41