3

I have a simple table. I found that some of the Mathematical expressions cross the border of the cells. How to fix this problem?

I have the second question please which is also related to my table: I have merged five cells; how I can split it into two equal cells?

Thanks in advance.

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{caption}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\begin{document}
%
\begin{table}[]
\centering
\caption{First Table}
\label{my table}
\begin{tabular}{|l|l|l|c|c|l|}
\hline
Name & \multicolumn{1}{c|}{a}               & \multicolumn{1}{c|}{b} & c                     & \multicolumn{1}{l|}{Test-1} & Test-2                                      \\ \hline
1    & aa                                   & bb                     & \multirow{5}{*}{same} & \multirow{2}{*}{222-a}      & \multicolumn{1}{c|}{\multirow{2}{*}{333-a}} \\ \cline{1-3}
2    & cc                                   & dd                     &                       &                             & \multicolumn{1}{c|}{}                       \\ \cline{1-3} \cline{5-6} 
3    & ee                                   & ff                     &                       & \multirow{3}{*}{222-b}      & \multirow{3}{*}{333-b}                      \\ \cline{1-3}
4    & $\sin^{2}\left(\frac{\pi}{M}\right)$ & gg                     &                       &                             &                                             \\ \cline{1-3}
5    & hh                                   & ii                     &                       &                             &                                             \\ \hline
\end{tabular}
\end{table}
%
\end{document}

Formulas inside a table

sky-light
  • 241
  • 2
  • 6
  • 13
  • 1
    please post a complete MWE. i.e. including eveything that can be copied and compiled as is. – Nasser Feb 04 '17 at 20:36
  • 1
    I have the second question please which is also related to my table it is best to post each question separately. – Nasser Feb 04 '17 at 20:39
  • @Nasser I have provided the complete table, what should I provide more? – sky-light Feb 04 '17 at 20:42
  • @sky-light We really need a full document, starting \documentclass and ending \end{document}, to see what you are loading, setting, etc. – Joseph Wright Feb 04 '17 at 20:49

2 Answers2

8

When it comes to writing mathematical formulas inside tables, your first impulse should be to treat it as if it were located in running, "inline" text. In particular, avoid \frac expressions and use in-fix "slash" notation instead.

Also, do try to give your tables a more "open" look. This is achieved easily simply by getting rid of all vertical lines; see below. Trust me, the vertical lines aren't needed, and they won't be missed.

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\begin{document}

\begin{table}
\setlength\extrarowheight{1pt}
\centering
\caption{First Table}
\label{my table}
\begin{tabular}{lccccc}
\hline
Name & a & b & c& Test-1 & Test-2 \\ 
\hline
1 & aa & bb& & \multirow{2}{*}{222-a} & \multirow{2}{*}{333-a} \\ 
\cline{1-3}
2 & cc & dd \\ 
\cline{1-3} \cline{5-6} 
3 & ee & ff & same \\ 
\cline{1-3}
4 & $\sin^{2^{\mathstrut}}(\pi/M)$ & gg & 
& 222-b & 333-b \\ 
\cline{1-3}
5 & hh & ii \\ 
\hline
\end{tabular}
\end{table}
\end{document}
Mico
  • 506,678
6

You can make latex tables less tight, either changing the value of \arraystretch or using the \setcellgapes command, from makecell, which adds a fixed height and depth to all cells in a table. Of course, \multirow then requires some small adjustment.

\documentclass{article}
\usepackage{amsmath, array,multirow, makecell, caption}
\usepackage{cellspace} %
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}


\begin{document}

\begin{table}[!htbp]
  \centering
  \caption{First Table}
  \label{my table}\renewcommand\arraystretch{1.3}
  \begin{tabular}{|l|l|l|c|c|l|}
    \hline
    Name & \multicolumn{1}{c|}{a} & \multicolumn{1}{c|}{b} & c & \multicolumn{1}{l|}{Test-1} & Test-2 \\ \hline
    1 & aa & bb & \multirow{5}{*}{same} & \multirow{2}{*}{222-a} & \multicolumn{1}{c|}{\multirow{2}{*}{333-a}} \\ \cline{1-3}
    2 & cc & dd & & & \multicolumn{1}{c|}{} \\ \cline{1-3} \cline{5-6}
    3 & ee & ff & & \multirow{3}{*}{222-b} & \multirow{3}{*}{333-b} \\ \cline{1-3}
    4 & $\sin^{2}\left(\frac{π}{M}\right)$ & gg & & & \\ \cline{1-3}
    5 & hh & ii & & & \\ \hline
  \end{tabular}
\end{table}

\begin{table}[!htbp]
  \centering
  \caption{First Table}
  \label{my table}\setcellgapes{3pt}\makegapedcells
  \begin{tabular}{|l|l|l|c|c|l|}
    \hline
    Name & \multicolumn{1}{c|}{a} & \multicolumn{1}{c|}{b} & c & \multicolumn{1}{l|}{Test-1} & Test-2 \\ \hline
    1 & aa & bb & \multirow{5}{*}[-0.72ex]{same} & \multirow{2}{*}{222-a} & \multicolumn{1}{c|}{\multirow{2}{*}{333-a}} \\ \cline{1-3}
    2 & cc & dd & & & \multicolumn{1}{c|}{} \\ \cline{1-3} \cline{5-6}
    3 & ee & ff & & \multirow{3}{*}[-.75ex]{222-b} & \multirow{3}{*}[-.75ex]{333-b} \\ \cline{1-3}
    4 & $\sin^{2}\left(\frac{π}{M}\right)$ & gg & & & \\ \cline{1-3}
    5 & hh & ii & & & \\ \hline
  \end{tabular}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
  • Instead of writing \multirow{5}{*}{same} or \multirow{5}{*}[-0.72ex]{same} for the material in column "c", wouldn't it be easier to just write "same" in row "3"/column "c"? – Mico Feb 04 '17 at 21:40
  • @Mico: I thought of it, but decided (in a silly way?) to fit the O.P.'s choice. But you're perfectly right! A last tghought: would that mean we should use \multirow{n} only for even ns? – Bernard Feb 04 '17 at 21:46
  • As long as the rows are of roughly equal height -- as is the case here -- I'd agree with the "last thought". In fact, it's the reason why I have only one pair of \multirow statements in my parallel answer. :-) – Mico Feb 04 '17 at 21:54