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

I need this to fit in page, but it overflows! I tried reference on the link

but no success. Here's the table.

\begin{center}
\begin{tabular}{|1|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}

• How is the \tabcap macro defined? And, did you mean to write l ("ell") rather than 1 ("one") in the argument of \begin{tabular}? – Mico Apr 26 '16 at 19:43
• l (not (1) and c are single line entries: perhaps you want p{3cm} to allow line breaking but you ask how to make the table fit without giving any indication of the size that it needs to fit into. Please always post a complete small docuemnt that demonstrates the problem. – David Carlisle Apr 26 '16 at 19:47

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 \\
1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
1  & Arduino   & Code burning and executing & Pin controlled write & blinking LED on intended pin & Blinking of LED \\
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}


• does \documentclass{} matter? – T3J45 Apr 27 '16 at 2:59
• I want table to continue in second page, when I use \newpage it gives back an error. any ideas? – T3J45 Apr 27 '16 at 3:34

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}


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.

\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\\