4

For the first column, I want the header cell to be centered but the other rows to be left-aligned with some space before. How to achieve this ? Can I define different spacing for the same column in multiple rows for a LaTeX table ? The column width specifications using \hsize in tabularx should not be disturbed.

\documentclass[conference]{IEEEtran}
\usepackage{tabularx}
\usepackage{booktabs}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\centering

\begin{tabularx}{\columnwidth}
{ @{\hspace{.2em}} >{\hsize=1.5\hsize}L *{4}{>{\hsize=0.875\hsize}C} @{} }
\toprule
\centering\arraybackslash\textbf{Optimization Algorithm} & \textbf{Swarm Size} & \textbf{No. of Iterations} & \textbf{Final Wirelength} & \textbf{Best Value Cost Fn.}\\
\midrule
\hspace{.5em} Short Text                      & 10    & 1000  & 100   & --    \\
\hspace{.5em} Slightly Longer Text that Wraps & 20    & 2000  & 200   & 10.5  \\
\hspace{.5em} Slightly Longer Text that Wraps & 30    & 3000  & 300   & 20.55 \\
\hspace{.5em} Short                           & 40    & 4000  & 400   & 30.5  \\
\bottomrule
\end{tabularx}

\end{table}

\end{document}

MWE Output

EDIT: Since my question was misunderstood, let me restate that what I want is some alternative to \hspace that adds space before every line in the paragraph, and no indent for the first line. Or maybe indent all lines. Illustrated below, I want all text to start immediately at the right of the red line. My problem is where the text wraps and \hspace cannot help me there.

Manually adding space after the text wrap is not an acceptable solution. I want automated cell padding or paragraph padding/reshape that affects all lines of the paragraph.

Desired Output Hint

  • Though this may be for another question, adding decimal alignment would be a bonus. Please look at this answer to see where the query is coming from. tabularx allows relative width specifications with X and siunitx allows decimal alignment with S. How to do both ? – geekshift Dec 27 '16 at 20:23
  • Thanks for clarifying your objective with regard to the formatting of the material in the first column. Real quick: Is it coincidence that the desired indentation of the "ordinary" rows is the same as the indentation of the longest word in the header cell? Please advise. – Mico Dec 28 '16 at 7:48
3

The following solution utilizes a tabularx environment, S column types for the numbers in the data columns, and modified X column types for the header cells above the data columns. It also automates the automatic indentation of the first row in the left-hand descriptor column.

enter image description here

\documentclass[conference]{IEEEtran}
\usepackage{tabularx,booktabs,siunitx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mC[1]{\multicolumn{1}{>{\hsize=0.875\hsize}C}{#1}} % handy shortcut macro

\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
%%\centering % not needed

\begin{tabularx}{\columnwidth}{@{} 
     >{\hspace{0.75em}\hsize=1.5\hsize}L 
     S[table-format=2.0]
     S[table-format=4.0]
     S[table-format=3.0]
     S[table-format=2.2] @{}}
\toprule
\multicolumn{1}{@{}>{\hsize=1.5\hsize}C}{\textbf{Optimization Algorithm}} & 
\mC{\textbf{Swarm Size}} & 
\mC{\textbf{No.\ of Iterations}} & 
\mC{\textbf{Final Wirelength}} & 
\mC{\textbf{Best Value Cost Fn.}}\\
\midrule
Short Text                      & 10 & 1000 & 100 & {--} \\
Slightly Longer Text that Wraps & 20 & 2000 & 200 & 10.5 \\
Slightly Longer Text that Wraps & 30 & 3000 & 300 & 20.55\\
Short                           & 40 & 4000 & 400 & 30.5 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document} 

Addendum to address the OP's follow-up inquiry: If what you want is to pad the left-hand edge of the table with a certain amount of whitespace, the following code may be of relevance:

enter image description here

\documentclass[conference]{IEEEtran}
\usepackage{tabularx,booktabs,siunitx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mC[1]{\multicolumn{1}{>{\hsize=0.875\hsize}C}{#1}} % handy shortcut macro

\begin{document}
\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\setlength\tabcolsep{2pt}  % default value: 6pt
\caption{Comparison of Algorithm Results}\label{tab:alg_results}

\begin{tabularx}{\columnwidth}{@{\hspace{1.3em}} % choose amount of indentation
     >{\hsize=1.5\hsize}L 
     S[table-format=2.0]
     S[table-format=4.0]
     S[table-format=3.0]
     S[table-format=2.2] @{}}
\toprule
%% disable left-hand padding in first cell via "@{}" particle
\multicolumn{1}{@{}>{\hsize=1.5\hsize}C}{\textbf{Optimization Algorithm}} & 
\mC{\textbf{Swarm Size}} & 
\mC{\textbf{No.\ of Iterations}} & 
\mC{\textbf{Final Wirelength}} & 
\mC{\textbf{Best Value Cost Fn.}}\\
\midrule
Short Text                      & 10 & 1000 & 100 & {--} \\
Slightly Longer Text that Wraps & 20 & 2000 & 200 & 10.5 \\
Slightly Longer Text that Wraps & 30 & 3000 & 300 & 20.55\\
Short                           & 40 & 4000 & 400 & 30.5 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document} 
  • I appreciate your response. But my query was padding all the lines in the table cell not indenting the first line. \hspace only adds space to the beginning of the text and my problem is where the text wraps. Please note that manually adding \hspace before every line in the paragraph is not what I'm looking for but automating the adding of such space wherever the text wraps, altering the shape of the paragraph or introducing padding to the paragraph to rephrase it. – geekshift Dec 28 '16 at 7:21
  • @geekshift - I've provided an addendum in which (I hope) I've addressed your follow-up inquiry. – Mico Dec 28 '16 at 8:00
  • I was looking to add padding only to the left-aligned cells and not to the table heading 'Optimization Algorithm' which has now shifted right. Or you could add right padding to 'Optimization Algorithm' to make it align with the left-aligned text data (see red line carefully - all text of 1st column should start from it including heading). This means anyway to address my objective, I need variable padding for a column and not same padding for the whole column as you specify in the preamble. – geekshift Dec 28 '16 at 8:09
  • @geekshift - If the header cell's material shouldn't be padded on the left, simply replace \multicolumn{1}{>{\hsize=1.5\hsize}C}{...} with \multicolumn{1}{@{}>{\hsize=1.5\hsize}C}{...}. – Mico Dec 28 '16 at 8:12
  • Padding specific cells is what I am looking for. This is quite easy to achieve in Word by changing the Left Indent of a particular cell. – geekshift Dec 28 '16 at 8:12
3

enter image description here

\documentclass[conference]{IEEEtran}
\usepackage{siunitx}
\usepackage{booktabs, makecell, tabularx}
    \renewcommand\theadfont{\bfseries}

%\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[!h]
\caption{Comparison of Algorithm Results}
    \renewcommand{\arraystretch}{1.1}
    \setlength\tabcolsep{3pt}
    \centering
\begin{tabularx}{\columnwidth}{@{}L S[table-format=2.0]
                                 S[table-format=4.0]
                                 S[table-format=3.0]
                                 S[table-format=2.2]@{}}
    \toprule
\thead[b]{Optimization\\ Algorithm} 
        &   {\thead[b]{Swarm\\ Size}} 
            &   {\thead[b]{No. of\\ Iterations}} 
                &   {\thead[b]{Final\\ Wire\\ length}} 
                    &   {\thead[b]{Best Value\\ Cost Fn.}}      \\
    \midrule
Short Text                      & 10    & 1000  & 100   & {--}  \\
Slightly Longer Text that Wraps & 20    & 2000  & 200   & 10.5  \\
Slightly Longer Text that Wraps & 30    & 3000  & 300   & 20.55 \\
Short                           & 40    & 4000  & 400   & 30.5  \\
    \bottomrule
\end{tabularx}
\end{table}

In comparison to your MWE are in above two major changes:

  • for column head is used macro \thead from makecell package (added to preamble)
  • for columns of numbers is used S column type from package siunitx (also added to preamble)

Beside this changes, I reduce arraystretch and tabcolsep and deleted change to X column type.

  • Though this doesn't directly address my query (see edit), +1 for nice formatting and makecell. I'd appreciate any links to interesting/proper demonstrations of makecell or any suggested tutorials. – geekshift Dec 28 '16 at 9:20

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.