0

I'm asking for your help regarding the creation of a table as such :

    \documentclass[a4paper,12pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{makecell} 
    \usepackage{tabularx}
    \RequirePackage{geometry}
    \geometry{a4paper, top=2cm, bottom=2cm, left=2cm, right=2cm}


    \begin{document}
    \begin{tabularx}{\linewidth}{ |*{3}{X|} X| *{3}{X|} X| *{3}{X|} X 
                ||  c  || c ||          
                *{16}{X|} X 
                |c}
        \hline
        \multicolumn{12}{|c||}{\textbf{Base 2}}     
        & \textbf{Base 16}
        & \textbf{Base 10}
        & \multicolumn{16}{c|}{ \textbf{DCB} } &
        \\ \hline \hline
        &&&&& &&&&& & 
        &
        &
        &&&&& &&&&& &&&&& &\\ \hline
    \end{tabularx}
    \end{document}

This code produces :

Table with one useless row

Notice the last column that I don't want in my table but I added to show you the difference it makes when I remove it, the code becomes :

    \begin{tabularx}{\linewidth}{ |*{3}{X|} X| *{3}{X|} X| *{3}{X|} X 
                ||  c  || c ||          
                *{16}{X|} X 
                |}
        \hline
        \multicolumn{12}{|c||}{\textbf{Base 2}}     
        & \textbf{Base 16}
        & \textbf{Base 10}
        & \multicolumn{16}{c|}{ \textbf{DCB} } 
        \\ \hline \hline
        &&&&& &&&&& & 
        &
        &
        &&&&& &&&&& &&&&& \\ \hline
    \end{tabularx}

Which produces :

Out of bound table

And there everything went to s**t

  • The cell contaning "DCB" is no longer aligned to the 16 cell beneath it
  • The small cells expand
  • The table goes out of bound

Can you help me ?

2
  • 1
    There seems to be no reason to use tabularx here as there is no line breaking within the table (I wouldn't use it, and I wrote it:-) But the main issue here is a low level optimisation in tex that columns that are spanned in every row are essentially removed, which is why you need the full row at the end otherwise some columns get removed and then tabularx's column assignments go way off. Mar 9, 2020 at 15:59
  • what do you mean by "remove the last row" ? both your code fragments have a two-row tablularx? oh, you meant last column (|c) Mar 9, 2020 at 16:03

1 Answer 1

2

I wouldn't use tabularx here, that is designed to control line breaking within cells with paragraphs of text.

enter image description here

\documentclass[a4paper,12pt]{article}

    \usepackage{array}
    \usepackage{geometry}
    \geometry{a4paper, top=2cm, bottom=2cm, left=2cm, right=2cm}


    \begin{document}
    \begin{center}

X\dotfill X

     \setlength\tabcolsep{2pt}
    \begin{tabular}{
@{}
 |*{12}{wc{3mm}|}|
*{2}{c||}
*{16}{wc{3mm}|}
@{}
}
        \hline
        \multicolumn{12}{|c||}{\textbf{Base 2}}     
        & \textbf{Base 16}
        & \textbf{Base 10}
        & \multicolumn{16}{c|}{ \textbf{DCB} }
        \\ \hline \hline
        &&&&& &&&&& & 
        &
        &
        &&&&& &&&&& &&&&& &\\ \hline
    \end{tabular}

    \end{center}

    \end{document}

If you have an old copy of the array package and w is not defined, this definition of w would work

 \newcolumntype{w}[2]{>{\begin{lrbox}{0}}#1<{\end{lrbox}\makebox[#2]{\usebox0}}}
4
  • I can't compile it. If I just copy your code I get an error : Package array Error: Illegal pream-token (w): `c' used.
    – LMT-PhD
    Mar 9, 2020 at 19:13
  • @LMT-PhD oh sorry you must have an old copy of the array package updating the package and/or your tex installation would fix it or I could give an answer for an old array package later I suppose:-) what tex system do you have? Mar 9, 2020 at 19:16
  • 1
    @LMT-PhD I extended the answer with a definition of w for old copies of array Mar 9, 2020 at 19:48
  • Thank you, I'll also update my Tex version
    – LMT-PhD
    Mar 9, 2020 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .