latex padding / margin hell

13,586

Solution 1

You should be fine to just specify how you want the body cells formated and change only the headers to centered with multicolumn. That way you don't have to mess with too many cells like you do right now.

\documentclass{article}

\begin{document}
\begin{table}
  \centering
  \begin{tabular}{l|p{2cm}|p{2cm}}
    \multicolumn{1}{c}{A} &
    \multicolumn{1}{c}{B} &
    \multicolumn{1}{c}{C} \\
    1 & {Some longer text} & {Some other text}\\
    2 & {Some longer text} & {Some other text}\\
    3 & {Some longer text} & {Some other text}\\
    4 & {Some longer text} & {Some other text}
  \end{tabular}
\end{table}
\end{document}

Solution 2

Building on @honk's example, i think you'd want this:

\documentclass{article}

\begin{document}
\begin{table}
  \centering
  \begin{tabular}{>{\raggedright}l|>{\raggedright}p{2cm}|>{\raggedright}p{2cm}|}
    \multicolumn{1}{c}{A} &
    \multicolumn{1}{c}{B} &
    \multicolumn{1}{c}{C} \\
    1 & Some longer text & Some other text\\
    2 & Some longer text & Some other text\\
    3 & Some longer text & Some other text\\
    4 & Some longer text & Some other text \\
  \end{tabular}
\end{table}
\end{document}

Words still won't be hyphenated, but this should solve the blank line problem you're having.

Solution 3

Not the most elegant solution, but the \centering and \raggedright commands corresponds to the center and flushleft environments and don't come with the increased margins.

If I come across a nicer solution I will post it, but this could work I think.

Share:
13,586
D.C.
Author by

D.C.

Updated on June 04, 2022

Comments

  • D.C.
    D.C. about 2 years

    I have been wrestling with a latex table for far too long. I need a table that has has centered headers, and body cells that contain text that may wrap around. Because of the wrap-around requirement, i'm using p{xxx} instead of l for specifying cell widths. The problem this causes is that cell contents are not left justified, so the look like spaced-out junk. To fix this problem I'm using \flushleft for each cell. This does left justify contents, but puts in a ton of white space above and below the contents of the cell. Is there a way to stop \flushleft (or \center for that matter) to stop adding copious amounts of verical whitespace?

    thanks

    \begin{landscape}
        \centering
        % using p{xxx} here to wrap long text instead of overflowing it
        \begin{longtable}{ | p{4cm} || p{3cm} | p{3cm} | p{3cm} | p{3cm} | p{3cm} |}
            \hline
             & 
            % these are table headings.  the \center is causing a ton of whitespace as well
            \begin{center} \textbf{HTC HD2} \end{center} & 
            \begin{center} \textbf{Motorola Milestone} \end{center} & 
            \begin{center} \textbf{Nokia N900} \end{center} & 
            \begin{center} \textbf{RIM Blackberry Bold 9700} \end{center} &
            \begin{center} \textbf{Apple iPhone 3GS} \end{center} \\
            \hline
            \hline
            % using flushleft here to left-justify, but again it is causing a ton of white space above and below cell contents.
            \begin{flushleft}OS / Platform \end{flushleft}& 
            \begin{flushleft}Windows Mobile 6.5 \end{flushleft}& 
            \begin{flushleft}Google Android 2.1 \end{flushleft}& 
            \begin{flushleft}Maemo \end{flushleft}& 
            \begin{flushleft}Blackberry OS 5.0 \end{flushleft}& 
            \begin{flushleft}iPhone OS 3.1 \end{flushleft} \\           
            \hline
    

    Edit:

    Thanks for the answers so far. I thought I found a solution that works, but these problems still exist:

    • using \raggedright stops long words from being hyphenated. This looks pretty bad when a cell contains a bunch of lines with only one word in them.
    • using \raggedright only works if you end it with a blank line. This causes every cell to have a completely blank line at the bottom of it. This was what i was trying to avoid in the first place.

          {Display} & 
          {\raggedright 4.3 inch, 800 x 400 resolution} & 
          {\raggedright 3.7 inch, 854 x 480 resolution} & 
          {\raggedright 3.5 inch, 800 x 480 resolution} & 
          {\raggedright 2.44 inch, 320 x 480 resolution} & 
          {\raggedright 3.5 inch, 480 x 320 resolution} \\            
          \hline
      
  • D.C.
    D.C. about 14 years
    can you post a snippet that shows how to use ruggedright? somebody else suggested it as well, but I have had no luck getting it to work. Does it need to be inside an environment or something? Thanks for the help
  • D.C.
    D.C. about 14 years
    nice, I got this to work with raggedright. Incredibly hacky though, i had to add an extra blank line after each string of text, and wrap everything with {}.... oh latex.
  • Pieter
    Pieter about 14 years
    I was pondering about (a variant on) this approach but not sure whether it would work. Thanks for clearing this out. It sure is a more elegant solution than mine.