How to align table so that it doesn't overflow the page horizontally

Two solutions based on tabularx. The second uses the booktabs package, with only $3$ horizontal rules:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{array, caption, tabularx,  ragged2e,  booktabs}

\begin{document}\

\noindent\setlength\tabcolsep{4pt}%
\begin{tabularx}{\linewidth}{|l|c|*{4}{>{\RaggedRight\arraybackslash}X|}}
  \hline
  ID & Test Name & Test Description           & Input                & Expected Output              & Actual Output   \\ [0.5ex]
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \hline
\end{tabularx}
\captionof{table}{Sound Limit in different zones}
\vskip1cm

\noindent\setlength\tabcolsep{4pt}%
\begin{tabularx}{\linewidth}{lc*{4}{>{\RaggedRight\arraybackslash}X}}
  \toprule
  ID & Test Name & Test Description           & Input                & Expected Output              & Actual Output   \\ [0.5ex]
  \midrule
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \addlinespace
  1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
  \bottomrule
\end{tabularx}
\captionof{table}{Sound Limit in different zones}

\end{document} 

enter image description here


Short of totally reformulating your table, there are several possible steps that can help, as shown below:

1) You can reduce the font size of the table

2) You can reduce the blank padding of each column by respecifying the length \tabcolsep.

3) If symmetric margin overrun is permitted (it is in my organization), you can place the tabular into a \makebox of width less than or equal the \textwidth, which will force any margin overlaps to be symmetrically distributed left and right, rather than run off the right margin.

Here is the MWE. Note: since \tabcap macro was not provided, I changed over to \captionof of the caption package.

\documentclass{article}
\usepackage{caption,lipsum}
\begin{document}
\begin{center}
 \tabcolsep=1pt\relax
 \small% <<--- CAN USE \footnotesize OR \scriptsize IF NEEDED
\captionof{table}{Sound Limit in different zones}
 \makebox[\textwidth]{\begin{tabular}{|l|c|c|c|c|c|} 
 \hline
 ID & Test Name & Test Description & Input & Expected Output & Actual Output \\ [0.5ex]
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\ 
 \hline
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \hline
\end{tabular}}
%\tabcap{Sound Limit in different zones}
\end{center}\medskip
\lipsum[1]
\end{document}

enter image description here

While there are ways of scaling the tabular content to precisely fit the text margins, I do not recommend such an approach in general, because it means that each oversized table is presented it its own unique font size. If there is more than such table in a document, it can be visually jarring to see a different scale factor for each table.


I suggest you (a) use a tabularx environment -- with width set to \textwidth -- instead of a tabular environment, (b) use a raggedright version of the X column type for columns 3 thru 6, and (c) reduce the amount of intercolumn whitespace. You may also want to think about removing all vertical bars and using fewer (but well-spaced) horizontal bars in order to give the table a more open look.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\begin{document}
\begin{table}
\setlength\tabcolsep{3pt} % default value: 6pt
\caption{Sound Limit in different zones}
\centering
 \begin{tabularx}{\textwidth}{@{} ll *{4}{L} @{}} 
 \toprule
 ID & Test Name & Test Description & Input & Expected Output & Actual Output \\ [0.5ex]
 \midrule
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\ 
 \addlinespace
 1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \addlinespace
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \addlinespace
1 & Arduino & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED\\
 \bottomrule
\end{tabularx}
\end{table}
\end{document}

Tags:

Tables