TransWikia.com

How to format this long table?

TeX - LaTeX Asked by OGC on March 22, 2021

With the current version of my codes, the table looks a bit off.

    documentclass{article}  
    usepackage{longtable}
    
    title{A longtable example}
    
    begin{document}
    begin{center}
    begin{longtable}[H]{|p{2.5cm}|p{1.2cm}|p{1.2cm}|p{1.2cm}|p{1.2cm}|p{1.2cm}|p{1.2cm}|}
        caption{WITS and ASUS Type of Treatment by Offender Type}
        label{test_table}  hline
        &multicolumn{3}{c|}{textbf{WITS}}&multicolumn{3}{c|}{textbf{ASUS}} hline
        textbf{Treatment textbackslash{} Offender Type} & textbf{Probationer} & textbf{Parolees} &textbf{Maximum-term Released Prisoners}  & textbf{Probationer} & textbf{Parolees} &textbf{Maximum-term Released Prisoners}  hline
        Outpatient&  &  &  & &  &  hline
        Intensive Outpatient&  &  &  &  &  & hline
        Day Treatment&  &  &  & &  &  hline
        Partial Hospitalization&  &  &  &  &  & hline
        Short Term Residential&  &  &  &  &  & hline
        Inpatient&  &  &  &  &  & hline
        Long Term Residential&  &  &  &  &  & hline
        Therapeutic Community&  &  &  &  &  & hline
        end{longtable}
    end{center}
    end{document}

See here for the output:

enter image description here

Any suggestions how I can format it and make it more presentable? I have been trying adjusting the widths of the table but to no avail.

EDIT: I tried something like this:

> %documentclass{article}
> %usepackage{longtable}
> 
> %title{A longtable example}
> 
> %begin{document}
> 
> %begin{center}
> begin{longtable}[H]{|p{2.5cm}|p{1.2cm}|p{1.2cm}|p{1.5cm}|p{1.2cm}|p{1.2cm}|p{1.5cm}|}
>   caption{WITS and ASUS Type of Treatment by Offender Type}
>   label{ASUS-WITS_treatment_table2}   hline
>   &multicolumn{3}{c|}{textbf{WITS}}&multicolumn{3}{c|}{textbf{ASUS}}
> hline    textbf{Treatment textbackslash{} Offender Type} &
> textbf{Prob.} & textbf{Parol.} &textbf{Max-term Rel. Pris.}  &
> textbf{Prob.} & textbf{Parol.} &textbf{Max-term Rel. Pris.} 
> hline    Outpatient&  &  &  & &  &  hline     Intensive Outpatient&  & 
> &  &  &  & hline   Day Treatment&  &  &  & &  &  hline  Partial
> Hospitalization&  &  &  &  &  & hline  Short Term Residential&  & 
> &  &  &  & hline   Inpatient&  &  &  &  &  & hline  Long Term
> Residential&  &  &  &  &  & hline  Therapeutic Community&  &  &  & 
> &  & hline     multicolumn{7}{@{}p{textwidth}@{}}{Prob. =
> Probationer; Parol. = Parolees; Max.-term Rel. Pris. = Maximum-term
> Released Prisoners}     end{longtable} %end{center}
> 
> %end{document}

2 Answers

You had specified the table to be wider than the page, so I reduced the column widths.

It would look better with shorter top headings, but you need subject knowledge to pick better headings so I left them as they are.

enter image description here

documentclass{article}

usepackage{longtable,array}

title{A longtable example}

begin{document}
setlengthextrarowheight{2pt}
begin{longtable}{|>{raggedright}p{2.3cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|}
    caption{WITS and ASUS Type of Treatment by Offender Type}
    label{test_table}  hline
    &multicolumn{3}{c|}{textbf{WITS}}&multicolumn{3}{c|}{textbf{ASUS}} hline
    textbf{Treatment textbackslash{} Offender Type} & textbf{Prob-ationer} & textbf{Parol-ees} &textbf{Maxi-mum term Released Prisoners}  & textbf{Prob-ationer} & textbf{Parol-ees} &textbf{Maxi-mum term Released Prisoners}  hline
    Outpatientmbox {}&  &  &  & &  &  hline
    Intensive Outpatient&  &  &  &  &  & hline
    Day Treatment&  &  &  & &  &  hline
    Partial Hospitalization&  &  &  &  &  & hline
    Short Term Residential&  &  &  &  &  & hline
    Inpatientmbox{ }&  &  &  &  &  & hline
    Long Term Residential&  &  &  &  &  & hline
    Therapeutic Community&  &  &  &  &  & hline
    end{longtable}

noindent Xdotfill X

end{document}

Correct answer by David Carlisle on March 22, 2021

I propose this, which uses the xltabular environment, which combines the functionalities of longtable and tabularx.

Also, depending on the real contents of your table, you might consider having a landscape table.

documentclass[11pt, letterpaper]{article}
usepackage[margin=0.75in]{geometry}
usepackage{caption}
usepackage{longtable}
usepackage{xltabular}
renewcommand{tabularxcolumn}[1]{m{#1}}
usepackage{ragged2e}
newcolumntype{Y}{>{hsize=0.7hsizeRaggedRightarraybackslash}X}
title{A longtable example}

begin{document}

setlength{extrarowheight}{2pt}setlength{tabcolsep}{2pt}
captionsetup{width=linewidth}
begin{xltabular}{linewidth}{|>{hsize=1.6hsizeRaggedRight}X|*{2}{X|Y|X|}}
    caption{WITS and ASUS Type of Treatment by Offender Type}
    label{test_table}  hline
    &multicolumn{3}{c|}{textbf{WITS}}&multicolumn{2}{c|}{textbf{ASUS}} hline
    textbf{Treatment textbackslash{}newline Offender Type} & textbf{Probationer} & textbf{Parolees} &textbf{Maximum-term Released Prisoners} & textbf{Probationer} & textbf{Parolees} &textbf{Maximum-term Released Prisoners}  hline
    Outpatient& & & & & &  hline
    Intensive Outpatient& & & & & & hline
    Day Treatment& & & & & &  hline
    Partial Hospitalization& & & & & & hline
    Short Term Residential& & & & & & hline
    Inpatient& & & & & & hline
    Long Term Residential& & & & & & hline
    Therapeutic Community& & & & & & hline
    end{xltabular}

end{document} 

enter image description here

Answered by Bernard on March 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP