# How to adjust tabular width to math body as defined by geometry?

How to adjust tabular width to math body as defined by geometry? In the following example the table goes more to the right than the border of body as defined by geometry package.

\documentclass{article}

\usepackage[showframe]{geometry}

\usepackage{lipsum}

\begin{document}
\begin{center}
\begin{tabular}{|p{0.3\linewidth}|p{0.7\linewidth}|}
\hline
\lipsum[2] & \lipsum[2]\\\hline
\end{tabular}
\end{center}

\end{document}

-
That's a weird table. Do you simply want the text to be formatted like that? In that case, there are better options. – Alenanno Jan 10 at 14:35
@Alenanno no, that's just a minimal example. I have to reproduce a layout made by MS word, especially a table. – student Jan 10 at 14:37

As you can see from your image: there is space between your columns, each column has \tabcolsep either side so your table is

\textwidth+4\tabcolsep

if you had array package loaded then rules would take width and it would be

\textwidth+4\tabcolsep+3\arrayrulewidth

so use

\dimen0=\textwidth-4\tabcolsep

\begin{tabular}{|p{.3\dimen0}|p{.7\dimen0}|}
-

You need to account for the fact that the table possesses three vertical bars (each of width \arrayrulewidth) and that each column has whitespace-padding on the left and right (in the amount of \tabcolsep).

You can either adjust the column widths manually, as is done in the first example below, or employ the tabularx package and set up the tabularx environment to have columns of relative widths 0.3 and 0.7. (Since there are two columns, the sums of the \hsize macros have to sum to 2.)

\documentclass{article}
\usepackage[showframe,vmargin=1cm]{geometry} % make both tables fit on one page
\usepackage{lipsum,tabularx}

\begin{document}
\noindent
\begin{tabular}{|
p{\dimexpr0.3\linewidth-2\tabcolsep-1.5\arrayrulewidth\relax}|
p{\dimexpr0.7\linewidth-2\tabcolsep-1.5\arrayrulewidth\relax}|}
\hline
\lipsum[2] & \lipsum[2]\\
\hline
\end{tabular}

\bigskip
\noindent
\begin{tabularx}{\textwidth}{|
>{\hsize=0.6\hsize}X|
>{\hsize=1.4\hsize}X|}
\hline
\lipsum[2] & \lipsum[2]\\
\hline
\end{tabularx}

\end{document}
-
The tabularx package implicitly performs the calculation shown in David's answer, i.e., \dimen0=\dimexpr\textwidth-4\tabcolsep-3\arrayrulewidth\relax. The code shown above then sets the column widths to 0.3\dimen0 and 0.7\dimen0, respectively. – Mico Jan 10 at 14:44
note that tabularx loaded array so changed the width of the table: vertical rules take up no horizontal space in the original latex implementation. – David Carlisle Jan 10 at 15:43