Create tabular/table with exact width

Try this simple code:

\documentclass[varwidth=3.5cm,margin=2mm]{standalone}
\usepackage{tabularx}
\setlength{\arrayrulewidth}{.6pt}
\setlength{\tabcolsep}{0pt}

\begin{document}

\textbf{tabularx package}\par\vspace{5pt}
\begin{tabularx}{14mm}{ |X|X| }
    \hline
    \hspace{2pt}foo & \hspace{2pt}bar \\
\hline
\end{tabularx} (1)
\par\vspace{5pt}

\end{document}

enter image description here


It's not completely clear what the requirements are but as far as I can see you want a table with two equal columns, three vertical rules and a total width of 14mm, for that I would do the following.

enter image description here

\documentclass{article}
\usepackage{lmodern}

\usepackage{array}
\newlength\zz

\begin{document}

\setlength\zz{\dimexpr 14mm - 4\tabcolsep-3\arrayrulewidth}
\setlength\tabcolsep{2pt}
\fontsize{3.5pt}{4pt}\selectfont


\begin{tabular}{|p{.5\zz}|p{.5\zz}|}
foo  &bar\\
zz&zzz
\end{tabular}

\end{document}

You could define a phantom vrule that takes the same space as a standard rule. Or the other way round a vrule that doesn't take space:

\documentclass{article}
\usepackage{array}
\setlength\arrayrulewidth{4pt} %for better view

\newcolumntype\pvline{!{\hspace{\arrayrulewidth}}}

\newcolumntype\vline{!{\hspace{-0.5\arrayrulewidth}\vrule width \arrayrulewidth\hspace{-0.5\arrayrulewidth}}}

\begin{document}
\begin{tabular}{p{\dimexpr1cm-2\tabcolsep}|p{\dimexpr1cm-2\tabcolsep}}
\hline
blb& blb \\
blb&blbl 
\end{tabular}

\begin{tabular}{p{\dimexpr1cm-2\tabcolsep}\pvline p{\dimexpr1cm-2\tabcolsep}}
\hline
blb& blb
\end{tabular}

\bigskip

\begin{tabular}{p{\dimexpr1cm-2\tabcolsep}\vline p{\dimexpr1cm-2\tabcolsep}}
\hline
blb& blb\\
blb&blbl
\end{tabular}

\begin{tabular}{p{\dimexpr1cm-2\tabcolsep} p{\dimexpr1cm-2\tabcolsep}}
\hline
blb& blb
\end{tabular}

\end{document}

enter image description here

Tags:

Spacing

Tables