2

I am trying to create a table using tabularx package. I want the first column to have a certain width (38 mm) and all other columns to have the another width (15 mm). But the last column seems to have a fixed value width, as figure 1 illustrates, no matter the value I set for it.

Figure illustrating what is happening

The code is:

\documentclass[12pt]{report}  
\usepackage{multirow,multicol}  
\usepackage{tabularx}

\begin{document}    

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

    \renewcommand{\arraystretch}{1.5} 

    % Definning column specifications:
    \renewcommand{\tabularxcolumn}[1]{>{\normalsize}m{#1}}
    \newcolumntype{A}{>{\normalsize\centering\arraybackslash}m{38mm}}
    \newcolumntype{B}{>{\normalsize\centering\arraybackslash}m{15mm}}

    \begin{table}[!hbt]
        \centering
        \caption{Shape functions for quadrilateral elements.} \label{tab.:2DQuadShapeFunc}~\\[-3mm]
        %\begin{tabular}{lc*{4}c}    
        \begin{tabularx}{\textwidth}{ABBBBB}
            \multicolumn{1}{c}{}                     & \multicolumn{5}{c}{Included only if node $i$ is defined} \\
            \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{$i=5$}             & \multicolumn{1}{c|}{$i=6$}   
            & \multicolumn{1}{c|}{$i=7$}             & \multicolumn{1}{c|}{$i=8$} 
            & \multicolumn{1}{c|}{$i=9$}             \\ \hline
            \multicolumn{1}{l|}{$h_1=\frac{1}{4}(1-r)(1-s)$} & \multicolumn{1}{c|}{$-\frac{1}{2}h_5$}   & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{}                  &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_8$}   &
            \multicolumn{1}{c|}{$-\frac{1}{4}h_9$}   \\
            \multicolumn{1}{l|}{$h_2=\frac{1}{4}(1+r)(1-s)$} & \multicolumn{1}{c|}{$-\frac{1}{2}h_5$}   &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_6$}   & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{}                  &
            \multicolumn{1}{c|}{$-\frac{1}{4}h_9$}   \\
            \multicolumn{1}{l|}{$h_3=\frac{1}{4}(1+r)(1+s)$} & \multicolumn{1}{c|}{}                    &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_6$}   &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_7$}   & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{$-\frac{1}{4}h_9$} \\
            \multicolumn{1}{l|}{$h_4=\frac{1}{4}(1-r)(1+s)$} & \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{$-\frac{1}{2}h_7$} &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_8$}   &
            \multicolumn{1}{c|}{$-\frac{1}{4}h_9$}   \\ \cline{1-1}
            $h_5=\frac{1}{2}(1-r^2)(1-s)$                    & \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{}                  & \multicolumn{1}{c|}{}      
            & \multicolumn{1}{c|}{$-\frac{1}{2}h_9$} \\ \cline{1-2}
            $h_6=\frac{1}{2}(1-s^2)(1+r)$                    &                                        & \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}      
            & \multicolumn{1}{c|}{}                  &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_9$}   \\ \cline{1-3}
            $h_7=\frac{1}{2}(1-r^2)(1+s)$                    &                                        &                   & \multicolumn{1}{c|}{}      
            & \multicolumn{1}{c|}{}                  &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_9$}   \\ \cline{1-4}
            $h_8=\frac{1}{2}(1-s^2)(1-r)$                    &                                        &                   &                            
            & \multicolumn{1}{c|}{}                  &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_9$}   \\ \cline{1-5}
            $h_9=(1-r^2)(1-s^2)$                             &                                        &                   &                            
            &                                        & \multicolumn{1}{c|}{}      
            \\ \hline
        \end{tabularx}
    \end{table}

    \renewcommand{\arraystretch}{1}

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

\end{document}

Can anyone help me out?

3

No need for tabularx! You overwrite the last column specification with c for every line, the reason why it doesn't use the m{1.5cm}:

\documentclass[12pt]{report}  
\usepackage{multirow,multicol}  
\usepackage{ragged2e,array}
\begin{document}    

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

    \renewcommand{\arraystretch}{1.5} 

    \newcolumntype{A}{>{\Centering}m{38mm}}
    \newcolumntype{B}{>{\Centering}m{15mm}}

\begin{table}[!hbt]
\centering
\caption{Shape functions for quadrilateral elements.}\label{tab.:2DQuadShapeFunc}~\\[-3mm]
        %\begin{tabular}{lc*{4}c}    
\begin{tabular}{A*5B | }
    & \multicolumn{5}{c}{Included only if node $i$ is defined} \\
 \multicolumn{1}{c|}{}   & 
\multicolumn{1}{c|}{$i=5$}             & \multicolumn{1}{c|}{$i=6$}   
    & \multicolumn{1}{c|}{$i=7$}             & \multicolumn{1}{c|}{$i=8$} 
    & $i=9$             \\ \hline
    \multicolumn{1}{l|}{$h_1=\frac{1}{4}(1-r)(1-s)$} & 
    \multicolumn{1}{c|}{$-\frac{1}{2}h_5$}   & \multicolumn{1}{c|}{}        
    & \multicolumn{1}{c|}{}                  &
    \multicolumn{1}{c|}{$-\frac{1}{2}h_8$}   &
    $-\frac{1}{4}h_9$   \\
\multicolumn{1}{l|}{$h_2=\frac{1}{4}(1+r)(1-s)$} & 
        \multicolumn{1}{c|}{$-\frac{1}{2}h_5$}   &
    \multicolumn{1}{c|}{$-\frac{1}{2}h_6$}   & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{}                  &
            $-\frac{1}{4}h_9$   \\
            \multicolumn{1}{l|}{$h_3=\frac{1}{4}(1+r)(1+s)$} & 
            \multicolumn{1}{c|}{}                    &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_6$}   &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_7$}   & \multicolumn{1}{c|}{}        
            &  $-\frac{1}{4}h_9$ \\
            \multicolumn{1}{l|}{$h_4=\frac{1}{4}(1-r)(1+s)$} & 
            \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{$-\frac{1}{2}h_7$} &
            \multicolumn{1}{c|}{$-\frac{1}{2}h_8$}   &
            $-\frac{1}{4}h_9$   \\ \cline{1-1}
            $h_5=\frac{1}{2}(1-r^2)(1-s)$                    & 
            \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}        
            & \multicolumn{1}{c|}{}                  & \multicolumn{1}{c|}{}      
            & $-\frac{1}{2}h_9$ \\ \cline{1-2}
            $h_6=\frac{1}{2}(1-s^2)(1+r)$                    
            &                                        & 
            \multicolumn{1}{c|}{}                    & \multicolumn{1}{c|}{}      
            & \multicolumn{1}{c|}{}                  &
            $-\frac{1}{2}h_9$   \\ \cline{1-3}
            $h_7=\frac{1}{2}(1-r^2)(1+s)$                    
            &                                        &                   & 
            \multicolumn{1}{c|}{}      
            & \multicolumn{1}{c|}{}                  &
            $-\frac{1}{2}h_9$   \\ \cline{1-4}
$h_8=\frac{1}{2}(1-s^2)(1-r)$ & & & &\multicolumn{1}{c|}{} &    $-\frac{1}{2}h_9$   \\ 
\cline{1-5}
            $h_9=(1-r^2)(1-s^2)$                             
            &                                        &                   
            &                            
            &                                        & \\ \hline
        \end{tabular}
    \end{table}

    \renewcommand{\arraystretch}{1}

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

\end{document}

enter image description here

It maybe easier to define the vertical lines and then use \multicolumn where it should not appear! Here I show it for the first seven lines:

\documentclass[12pt]{report}  
\usepackage{multirow,multicol}  
\usepackage{ragged2e,array}
\begin{document}    

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

    \renewcommand{\arraystretch}{1.5} 

\newcolumntype{A}{m{38mm}}
\newcolumntype{B}{>{\Centering}m{15mm}}
\let\MC\multicolumn

\begin{table}[!hbt]
\centering
\caption{Shape functions for quadrilateral elements.}\label{tab.:2DQuadShapeFunc}~\\[-3mm]
        %\begin{tabular}{lc*{4}c}    
\begin{tabular}{A | *5{B|} }
\MC{1}{c}{} & \multicolumn{5}{c}{Included only if node $i$ is defined} \\
    & $i=5$ & $i=6$ & $i=7$ & $i=8$ & $i=9$ \\ \hline
$h_1=\frac{1}{4}(1-r)(1-s)$ & $-\frac{1}{2}h_5$ & & & $-\frac{1}{2}h_8$ & 
    $-\frac{1}{4}h_9$ \\
$h_2=\frac{1}{4}(1+r)(1-s)$ & $-\frac{1}{2}h_5$ & $-\frac{1}{2}h_6$ & & & 
    $-\frac{1}{4}h_9$   \\
$h_3=\frac{1}{4}(1+r)(1+s)$ & & $-\frac{1}{2}h_6$ & $-\frac{1}{2}h_7$ & &   
    $-\frac{1}{4}h_9$ \\
$h_4=\frac{1}{4}(1-r)(1+s)$ & & & $-\frac{1}{2}h_7$ & $-\frac{1}{2}h_8$ &
    $-\frac{1}{4}h_9$   \\ \cline{1-1}
\MC{1}{l}{$h_5=\frac{1}{2}(1-r^2)(1-s)$} & & & & & $-\frac{1}{2}h_9$ \\ \cline{1-2}
\MC{1}{l}{$h_6=\frac{1}{2}(1-s^2)(1+r)$} & \MC{1}{c}{} & & & & $-\frac{1}{2}h_9$   \\ 
\cline{1-3}
\MC{1}{l}{$h_7=\frac{1}{2}(1-r^2)(1+s)$} & \MC{1}{c}{}& \MC{1}{c}{}& & & 
$-\frac{1}{2}h_9$   \\ \cline{1-4}
\end{tabular}
    \end{table}

    \renewcommand{\arraystretch}{1}

    Some text! Some text! Some text! Some text! Some text! Some text! Some 
    text! Some text!

\end{document}

enter image description here

2
  • First, I can see what you said I am overwriting and, second, how can I assure the table has the same width as the text? @Herbert – André Xavier Jul 19 '18 at 18:53
  • Then use tabularx with X instead of B – user2478 Jul 19 '18 at 19:12
2

with tabularx: first column width is set to desired 38 mm (however, to my opinion is better to use column type l, the width of rest of the columns are left to tabularx to calculate them:

\documentclass[12pt]{report}
\usepackage{makecell, multirow, tabularx}
\setcellgapes{3pt}
\let\MC\multicolumn
\usepackage{lipsum}

\begin{document}
\lipsum[11]


    \begin{table}[!hbt]
    \centering
\caption{Shape functions for quadrilateral elements.}
\label{tab.:2DQuadShapeFunc}
\makegapedcells
\begin{tabularx}{\linewidth}{>{\centering $}p{38 mm}<{$} |
                        *{5}{>{\centering\arraybackslash $}X<{$}|}}
\MC{1}{c}{} & \MC{5}{c}{Included only if node i is defined} \\
            & i=5               & i=6 & i=7 & i=8 & i=9                 \\
    \hline
h_1=\frac{1}{4}(1-r)(1-s)
            & -\frac{1}{2}h_5   & & & -\frac{1}{2}h_8 & -\frac{1}{4}h_9 \\
h_2=\frac{1}{4}(1+r)(1-s)
            & -\frac{1}{2}h_5   & -\frac{1}{2}h_6 & & & -\frac{1}{4}h_9 \\
h_3=\frac{1}{4}(1+r)(1+s)
            & & -\frac{1}{2}h_6 & -\frac{1}{2}h_7 &   & -\frac{1}{4}h_9 \\
h_4=\frac{1}{4}(1-r)(1+s)
            & & & -\frac{1}{2}h_7 & -\frac{1}{2}h_8   & -\frac{1}{4}h_9 \\
    \cline{1-1}
\MC{1}{l}{$h_5=\frac{1}{2}(1-r^2)(1-s)$}
            & & & & & -\frac{1}{2}h_9                                   \\
    \cline{1-2}
\MC{1}{l}{$h_6=\frac{1}{2}(1-s^2)(1+r)$}
            & \MC{1}{c}{} & & & & -\frac{1}{2}h_9                       \\
    \cline{1-3}
\MC{1}{l}{$h_7=\frac{1}{2}(1-r^2)(1+s)$}
            & \MC{2}{c}{}      & & & -\frac{1}{2}h_9           \\
    \cline{1-4}
    \end{tabularx}
\end{table}
\lipsum[12]

\end{document}

enter image description here

for more vertical space in cells is used macro \makegapedcells from the package makecell, removed are your column definitions (since they are not used)

note:

  • if you use tabularx at least of one column had to be X type
  • since macro \makegapedcells is not compatible with m column type, the redefinition of X columns \renewcommand{\tabularxcolumn}[1]{>{\normalsize}m{#1}} is deleted.
  • definition of new commands if they intended to be used globally (in whole document) should be in preamble (on this way is more simple to find them), locally is sensible to define them, if they should valid only in the environment, where are defined

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.