3

I want to create a table with fixed column lengths like this:

\documentclass[a4paper,12pt]{article}

\begin{document}

\begin{tabular}{|p{1cm}|p{2cm}|p{3cm}|}
    \hline
    1 cm  & 2 cm  & 3 cm  \\
    \hline 
\end{tabular}

Table with actual widths

\begin{tabular}{|p{0.56cm}|p{1.56cm}|p{2.56cm}|}
    \hline
    1 cm  & 2 cm  & 3 cm  \\
    \hline 
\end{tabular}

\end{document}

enter image description here

But column lengths are not the exact number, that I set. So I found the shift to make column with lengths, that I want.

Why this is happening and how I can automate the process of getting right value?

  • 2
    The argument of the p columns specifies the textwidth inside the cells, not the total width with padding around the text. – samcarter Jan 18 at 15:02
  • 2
    If you just for fun remove all other spaces \begin{tabular}{|@{}p{1cm}@{}|@{}p{2cm}@{}|@{}p{3cm}@{}|} you can see the effect – samcarter Jan 18 at 15:04
4
\documentclass[a4paper,12pt]{article}

\begin{document}

\setlength\arrayrulewidth{2pt}
\begin{tabular}{|p{\dimexpr1cm-2\tabcolsep}
                |p{\dimexpr2cm-2\tabcolsep}
                |p{\dimexpr3cm-2\tabcolsep}|}
    \hline
    1 cm  & 2 cm  & 3 cm  \\
    \hline
\end{tabular}

\rule{6cm}{2pt}
\end{document}

make absolute width of cells as you like to have. rule is comparison of the table width.

2

Almost the same as @Zarko's answer, except reducing the (fine) length of the line separating columns... and a introducing a customized macro:

\documentclass[a4paper,12pt]{article}
\usepackage{array, calc}

\newlength{\lw}
\setlength{\lw}{2\tabcolsep+\arrayrulewidth}

\begin{document}

\begin{tabular}{| p{1cm-\lw} | p{2cm-\lw} | p{3cm-\lw} |}
        \hline
        1 cm  & 2 cm  & 3 cm  \\
        \hline 
\end{tabular}
Table with actual widths
\begin{tabular}{|p{0.56cm}|p{1.56cm}|p{2.56cm}|}
        \hline
        1 cm  & 2 cm  & 3 cm  \\
        \hline 
\end{tabular}

\end{document}

producing :

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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