1

I'm having a little problems with tabularx package. So I'd like to make tables with a column that occupies all the remaining withe space, as a X column type does.

In the following MWE the last column width was set manually to 5 cm. There is a way to set this width that fills automatically all the remaining width of the page without using tabularx?

\documentclass[12pt,a4paper]{article}
\usepackage{blindtext}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm,
    showframe
]{geometry}
\usepackage{booktabs}
\usepackage{array}

\begin{document}
\begin{table}[h]
\centering
\caption{Caption}
\label{Label}
\begin{tabular}{lcp{5cm}}
\toprule
\textbf{Head 1} & \textbf{Head 2} & \textbf{Head 3} \\
\midrule
Row             & Row             & \blindtext             \\
Row             & Row             & Row             \\
Row             & Row             & Row             \\
Row             & Row             & Row             \\
Row             & Row             & Row             \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

EDIT 1: To show my problem with tabularx there is the real case in my thesis:

In my preamble:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{xfrac}
\usepackage{textcomp}
\usepackage[bitstream-charter]{mathdesign}
\usepackage{makeidx}
\usepackage{graphicx,graphics}
\usepackage{bookmark}
\usepackage[%
    left=3.00cm,
    right=2.00cm,
    top=3.00cm,
    bottom=2.00cm
]{geometry}
\usepackage[brazil]{babel} 
\usepackage[none]{hyphenat} 
\usepackage{multirow} 
\usepackage{multicol}
\usepackage{pdflscape} 
\usepackage{tabularx} 
\usepackage{subcaption} 
\usepackage{tocloft}
\usepackage[parfill]{parskip} 
\usepackage{icomma}
\usepackage[%
    inline,         
    shortlabels     
]{enumitem}
\usepackage{bigstrut}
\usepackage{booktabs}
\usepackage{afterpage}
\usepackage{rotating}
\usepackage{microtype}
\usepackage{tabto} 
\usepackage{expl3}
\ExplSyntaxOn
\tex_let:D \c_minus_one \scan_stop:
\int_const:Nn \c_minus_one {-1}
\ExplSyntaxOff
\usepackage{acro}
\usepackage[%
    font=footnotesize,      
    justification=centering,
    figurewithin=section,
    tablewithin=section
]{caption}
\usepackage{siunitx}
\usepackage{layouts} 
\usepackage{sectsty}
\usepackage{secdot}
\usepackage{mfirstuc}
\usepackage[export]{adjustbox}
\usepackage{array}
\usepackage{colortbl}
\usepackage{makecell}
\usepackage{ltablex}
\usepackage{threeparttable}
\usepackage[referable]{threeparttablex}
\usepackage{pdfpages}
\usepackage{chemformula}
\usepackage{footnotebackref}
\usepackage{footnotehyper}
\usepackage[%
backend=biber,
citestyle=authoryear-comp,  
bibstyle=authoryear,        
giveninits=true,            
maxcitenames=2,         
uniquelist=false,       
uniquename=false,       
sorting=ynt,            
sortcites,              
maxbibnames=99,         
url=false,              
eprint=false,           
dashed=false,           
]{biblatex}
\usepackage{setspace}
\usepackage{hyperref}
\usepackage[%
noabbrev,
nameinlink,
sort
]{cleveref}
\keepXColumns

The table with tabularx

\begin{table}
\footnotesize
\centering
\begin{tabularx}{\linewidth}{lcX}%
\caption{Locais e frequência de amostragem dos parâmetros monitorados em cada unidade do sistema de filtração}%
\label{tab:locais_e_freq}%
\\
\midrule
\textbf{\makecell{Parâmetro}}   & \textbf{\makecell{Local de\\amostragem}}  & \textbf{Frequência de amostragem} \\
\midrule
Turbidez                                & Afluente e efluente               & Diária                               \\
Cor aparente                            & Afluente e efluente               & Diária                               \\
Cor verdadeira                          & Afluente e efluente               & Diária                               \\
Coliformes totais                       & Afluente e efluente               & Diária                               \\
\Acf{ecoli}                             & Afluente e efluente               & Diária                               \\
\Acf{clorofilaa}                        & Afluente e efluente               & Diária                               \\
Densidade celular de
\acs{craciborskii}                      & Afluente e efluente               & ---                                  \\
\Acf{CYN}                               & Afluente e efluente               & Diária                               \\
Absorbância UV                          & Afluente e efluente               & $\rm{3\times semana}$                \\
pH                                      & Afluente                          & Diária                               \\
Temperatura                             & Afluente                          & Diária                               \\
Condutividade                           & Afluente                          & Diária                               \\
\Acf{OD}                                & Afluente                          & Diária                               \\
Alcalinidade                            & Afluente                          & $\rm{3\times semana}$                \\
\midrule
\end{tabularx}
\end{table}

The result doesn't respect the margin

enter image description here

When I create the table without tabularx

\begin{table}
\footnotesize
\centering
\caption{Locais e frequência de amostragem dos parâmetros monitorados em cada unidade do sistema de filtração}%
\label{tab:locais_e_freq}%
\begin{tabular}{lcp{6.5cm}}%
\midrule
\textbf{\makecell{Parâmetro}}   & \textbf{\makecell{Local de\\amostragem}}  & \textbf{Frequência de amostragem} \\
\midrule
Turbidez                                & Afluente e efluente               & Diária                               \\
Cor aparente                            & Afluente e efluente               & Diária                               \\
Cor verdadeira                          & Afluente e efluente               & Diária                               \\
Coliformes totais                       & Afluente e efluente               & Diária                               \\
\Acf{ecoli}                             & Afluente e efluente               & Diária                               \\
\Acf{clorofilaa}                        & Afluente e efluente               & Diária                               \\
Densidade celular de
\acs{craciborskii}                      & Afluente e efluente               & ---                                  \\
\Acf{CYN}                               & Afluente e efluente               & Diária                               \\
Absorbância UV                          & Afluente e efluente               & $\rm{3\times semana}$                \\
pH                                      & Afluente                          & Diária                               \\
Temperatura                             & Afluente                          & Diária                               \\
Condutividade                           & Afluente                          & Diária                               \\
\Acf{OD}                                & Afluente                          & Diária                               \\
Alcalinidade                            & Afluente                          & $\rm{3\times semana}$                \\
\midrule
\end{tabular}
\end{table}

The result is OK

enter image description here

  • 1
    Well you will have to measure somehow the width of the other columns, then you can calculate the width of the last column. Do you know the largest word in every column? – Ulrike Fischer Feb 18 at 20:44
  • @UlrikeFischer No, I don't. This is the reason for to do this – Daniel Valencia C. Feb 18 at 20:54
  • 2
    What are your problems with tabularx? – Bernard Feb 18 at 20:54
  • 1
    Your write-up sounds a little bit like you're trying to reinvent the wheel. Please tell us more about your "little problems with [the] tabularx package". It just may be lot more productive to those those pesky little problems... – Mico Feb 18 at 20:55
  • Then you need something like tabularx. – Ulrike Fischer Feb 18 at 21:01
3

An X column of the tabularx package is nothing but a p column for which LaTeX has done the width-related calculations.

In principle, it's possible to do these calculations by hand, although I can't imagine why somebody would want to do that. :-) Directives such as \newlength, settowidth, \setlength, \dimexpr, and \relax will quickly become your friends...

I won't post a screenshot to go with the following code.

\documentclass[12pt,a4paper]{article}
\usepackage[left=3cm,right=2cm,top=3cm,bottom=2cm]{geometry}
\usepackage{blindtext,booktabs,tabularx}
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{@{}ll X @{}}
\toprule
\textbf{Head 1} & \textbf{Head 2} & \textbf{Head 3} \\
\midrule
Row & Row & \blindtext \\
\bottomrule
\end{tabularx}


%% some tedious calculations:
\newlength{\lena}
\newlength{\lenb}
\newlength{\lenc}
\settowidth{\lena}{\textbf{Head 1}} % width of widest line in col. 1
\settowidth{\lenb}{\textbf{Head 2}} % width of widest line in col. 2
\setlength{\lenc}{\dimexpr\textwidth-\lena-\lenb-4\tabcolsep\relax}

\smallskip\noindent
\begin{tabular}{@{}ll p{\lenc} @{}}
\toprule
\textbf{Head 1} & \textbf{Head 2} & \textbf{Head 3} \\
\midrule
Row & Row & \blindtext \\
\bottomrule
\end{tabular}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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