Set table width same size of text width in LaTeX
In this tutorial, I will show you how to set a table width same as the text width (\textwidth
). You can do it with two methods as below:
- Set table width with
\resizebox
- Set table width with
tabularx
Set table width with \resizebox
In order to use this \resizebox
command, you have to load the graphicx
package. To set the table width same as the text width you have to use the tabular
environment inside the \resizebox command and use \textwidth command as an argument with this command. like this.
\resizebox{\textwidth}{!}{
\begin{tabular}
...
\end{tabular}
}
\documentclass{article} \usepackage{graphicx} \usepackage{lipsum} \begin{document} \lipsum[1][1-8] \begin{table}[h] \resizebox{\textwidth}{!}{ \begin{tabular}{|c|c|} \hline This is a sentence & This is a sentence too \\ \hline This is a sentence & This a sentence too \\ \hline \end{tabular} } \caption{Se the table width same as \textbf{textwidth}} \end{table} \end{document}
Output:
However, with this technique, you may face some font size issues.
Set table width with tabularx
This is the most recommended technique. First of all, you have to load the tabularx
package then you can use the tabularx
environment to create a table.
To set this table width the same as \textwidth
, you have to use \textwidth
as an argument with tabularx
and it also provides X column type, a new column type to fill out the rest of the space. Take a look.
\documentclass{article} \usepackage{tabularx} \usepackage{lipsum} \begin{document} \lipsum[1][1-8] \begin{table}[h] \begin{tabularx}{\textwidth}{|X|X|} \hline This is a sentence & This is a sentence too \\ \hline This is a sentence & This a sentence too \\ \hline \end{tabularx} \caption{Se the table width same as \textbf{tabularx}} \end{table} \end{document}
Output:
Leave a Reply