TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

enter image description here

share|improve this question
    
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}|}
share|improve this answer

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.)

enter image description here

\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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.