# Equations in tabularx

I have been trying to display a set of equations in a table using tabularx. However, none of the followings are satisfactory:

• The second row is too tight

• The third row has lots of spacing (I've tried other environments to remove spacing, none of them works. parbox gives a better result though, but not fulfilling either)

• and why both columns are the same size? (using align this will resolve, however spacing won't.)

 \documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document}

\begin{table}
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{\textbf{Species Concentration in the Bulk}} \\
\hline
$\left[H{\rm PO}_4^{2-}\right]_h$&
$\frac{{K_{a5}{K_{a6}\left[H_3O^+\right]}_h C}_{bp}}{{\left[H_3O^+\right]_h}^3+K_{a5}\left[H_3O^+\right]_h^2+K_{a5}{K_{a6}\left[H_3O^+\right]}_h +{K_{a5}K}_{a6}K_{a7}}$
\\\hline
$$\left[{\rm PO}_4^{3-}\right]_h$$&
$$\frac{{{K_{a5}K}_{a6}K_{a7}C}_{bp}}{{\left[H_3O^+\right]_h}^3+K_{a5}\left[H_3O^+\right]_h^2+K_{a5}{K_{a6}\left[H_3O^+\right]}_h +{K_{a5}K}_{a6}K_{a7}}$$\\
\hline
\end{tabularx}
\end{table}
\end{document}

• Using two X type columns inside of tabularx will by default result in two equally wide columns. – leandriis Apr 20 at 5:49
• Note that none of your \left... \right constructions here does anything relevant. Additionally see the mhchem package for a proper way to write H3O+ (hint they should be upright, mhchem makes that super easy) – daleif Apr 20 at 6:01
• Thanks for the notes, I know there are lots of non-sense terms in the equations, as they have been copied from word. – mmg Apr 20 at 7:09

For above result you need to define different width of columns, use the same math environments (\displaystyle) for all math terms and add some vertical space by employing cellspace package:

\documentclass{article}
\usepackage[column=O]{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\usepackage{tabularx}

\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{|>{\hsize=0.3\hsize$\displaystyle}X<{$}|
>{\hsize=1.7\hsize$\displaystyle}OX<{$}|}
\hline
\multicolumn{2}{|Ol|}{\textbf{Species Concentration in the Bulk}} \\
\hline
\bigl[H\mathrm{PO_4}^{2-}\bigr]_h
&   \frac{{K_{a5}{K_{a6}\left[H_3O^+\right]}_h C}_{bp}}
{\bigl[H_3O^+\bigr]_h^3 + K_{a5}\bigl[H_3O^+\bigr]_h^2 +
K_{a5}{K_{a6}\bigl[H_3O^+\bigr]}_h +
{K_{a5}K}_{a6}K_{a7}}
\\  \hline
\bigl[\mathrm{PO}_4^{3-}\bigr]_h
&   \frac{K_{a5}K_{a6} K_{a7}C_{bp}}
{\bigl[H_3O^+\bigr]_h^3 + K_{a5}\bigl[H_3O^+\bigr]_h^2 +
K_{a5}K_{a6} \bigl[H_3O^+ \bigr]_h +
K_{a5}K_{a6} K_{a7}}
\\
\hline
\end{tabularx}
\end{table}
\end{document}


Note: It is not clear what your math-mode symbols/variables mean. Somehow they remind me of chemical formulae. If this is case, then you may consider loading the mhchem package and, for example, replace the term in the first column and row as follows

% in preamble
\usepackage[version=4]{mhchem}

% in the first cell
\ce{[HPO4^{2-}]_h}


which gives (the cell with the \ce directive is highlighted in yellow):

• +1. I've taken the liberty of editing the \ce code a bit. Feel free to revert. – Mico Apr 20 at 7:33
• @Mico, thank you very much! Now is much better! – Zarko Apr 20 at 7:57

tabularx is designed to control the widths for linebreaking in paragraphs in p columns, so is not suitable here, I would use tabular. Also all X columns are the same width by default, but here you want cells of their natural width.

As a general comment there are packages such as mhchem to make entering chemical formual easier, but I did not change that here. Also, \rm is not defined by default in LaTeX.

\documentclass{article}
% only in old latex  \usepackage[utf8]{inputenc}
\usepackage{array,amsmath}
\newcommand\zfrac[2]{\dfrac{\rule[-5pt]{0pt}{0pt}#1}{\rule[-10pt]{0pt}{15pt}#2}}

\begin{document}

\begin{table}
\centering
\setlength\extrarowheight{2pt}
\renewcommand\arraystretch{2}
\begin{tabular}{|*{2}{>{$\displaystyle}c<{$}|}}
\hline
\multicolumn{2}{|c|}{\textbf{Species Concentration in the Bulk}} \\
\hline

\left[\mathrm{H}{\mathrm{PO}}_4^{2-}\right]_h
&

\zfrac{{\mathrm{K}_{a5}{\mathrm{K}_{a6}\left[\mathrm{H}_3O^+\right]}_h \mathrm{C}}_{bp}}{{\left[\mathrm{H}_3O^+\right]_h}^3+\mathrm{K}_{a5}\left[\mathrm{H}_3O^+\right]_h^2+\mathrm{K}_{a5}{\mathrm{K}_{a6}\left[\mathrm{H}_3O^+\right]}_h +{\mathrm{K}_{a5}\mathrm{K}}_{a6}\mathrm{K}_{a7}}

\\\hline

\left[{\mathrm{PO}}_4^{3-}\right]_h
&

\zfrac{{{\mathrm{K}_{a5}\mathrm{K}}_{a6}\mathrm{K}_{a7}\mathrm{C}}_{bp}}{{\left[\mathrm{H}_3O^+\right]_h}^3+\mathrm{K}_{a5}\left[\mathrm{H}_3O^+\right]_h^2+\mathrm{K}_{a5}{\mathrm{K}_{a6}\left[\mathrm{H}_3O^+\right]}_h +{\mathrm{K}_{a5}\mathrm{K}}_{a6}\mathrm{K}_{a7}}
\\
\hline
\end{tabular}
\end{table}
\end{document}
`