[Tex/LaTex] Left aligning cell contents in a table

horizontal alignmenthyphenationspacingtables

If you compile the code given below, then the row which states "Average Pre-Authentication Response Time" is justified with a lot of spacing between the words. I want it to be left-aligned. Also, if a line breakage is required, then can I ask LaTeX to prefer keeping the word as a whole and shifting it to the next line if space is available? In this case, there is a hyphen and the word is breaking, but there is enough space for the whole word to shift down.

\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%


\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage{graphicx,booktabs}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

%% for inline R code: if the inline code is not correctly parsed, you will see a message
\newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\colorlet{tableheadcolor}{blue!25}
\colorlet{fail}{red}
\colorlet{passPoor}{yellow}
\colorlet{passGood}{green!50}
\colorlet{optimal}{green}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addtolength{\oddsidemargin}{-.875in}
\addtolength{\evensidemargin}{-.875in}
\addtolength{\textwidth}{1.75in}
\addtolength{\topmargin}{-.875in}
\addtolength{\textheight}{1.75in}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}

\section{Summary}
\begin{flushleft}
\begin{table}[htbp]
  \small
  \centering
  \caption{Brief overview of the Results}
    \begin{tabularx}{0.75\textwidth}{XX}
    %\hline
    \toprule
    \rowcolor{tableheadcolor}
    Metrics for machine  & \textbf{Result} \\
    %\hline
    \midrule
    Failure Rate & 0.00\% \\
    Lorema LoremLore Lore & 0.0 sec \\
    Average Pre-Authentication (ASD) Response Time & 0.0 sec \\
    Overall Result & \cellcolor{passGood}Passed \\
    %\hline
    \bottomrule
    \end{tabularx}%
  \label{tab:overviewOld}%
\normalsize
\end{table}
\end{flushleft}

\end{document}

Best Answer

Collecting some of the information contained in the comments to the posting:

  • Text typeset in a column of type X is "fully justified". Depending on the (non)availabability of permissible hyphenation break points, this can lead to unsightly columns with either too little or too much text squeezed into the available space.

  • To typeset the text material in the first column of the table in "ragged-right" manner without permitting hyphenation, replace the column specifier X with

    >{\raggedright}X
    

    Think of the > symbol as "pushing" its argument, \raggedright, into the column specification of X.

  • A downside of using the \raggedright specification is that the resulting material can look, well, extremely ragged. To enable ragged-right typesetting while also allowing LaTeX to insert line breaks at hyphenation points within words, be sure to load the ragged2e package (which is already done in your code) and to use the column specification

    >{\RaggedRight}X
    

    Note the two uppercase-R letters.

Related Question