[Tex/LaTex] Math mode inside LyX tables

lyxtablesvertical alignment

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:

enter image description here

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

Best Answer

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:

enter image description here

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:

enter image description here

Giving the final, expected output:

enter image description here

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.

Related Question