3

I want to typeset a table and I used following code.

\documentclass{article}

\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|p{9.0cm}|p{1.5cm}|}
 \hline
User-agent string & dummy\\
 \hline
Googlebot (compatible; Googlebot/2.1; +http://www.google.com/bot.html)&10\\
Googlebot-Mobile/2.1&15\\
Googlebot/2.1 (+http://www.google.com/bot.html)&20\\
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)&40\\
Mozilla/5.0 (compatible; Googlebot/2.1;+http://www.google.com/bot.html)&50\\
 \hline
\end{tabular}
\end{table}

\end{document}

My table looks like following. enter image description here

How to remove unnecessary spacing in the text each row?

2 Answers2

3

Be sure to load the array package and replace p{9.0cm} with >{\raggedright}p{9.0cm}:

enter image description here

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|>{\raggedright}p{9.0cm}|p{1.5cm}|}
 \hline
User-agent string & dummy\\
 \hline
Googlebot (compatible; Googlebot/2.1; +http://www.google.com/bot.html)&10\\
Googlebot-Mobile/2.1&15\\
Googlebot/2.1 (+http://www.google.com/bot.html)&20\\
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)&40\\
Mozilla/5.0 (compatible; Googlebot/2.1;+http://www.google.com/bot.html)&50\\
 \hline
\end{tabular}
\end{table}
\end{document}

If you have several tables with columns with long lines that need to wrap and need to be set raggedright, you may want to provide the following command in the preamble (after loading the array package):

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

and use P in the definition of a tabular, say,

\begin{tabular}{|P{9.0cm}|p{1.5cm}|}

A feature of the \raggedright macro is that it doesn't allow hyphenation of words in its scope. If hyphenation is something you need, you should load the ragged2e package as well and define a column type named, say, Q:

\newcolumntype{Q}[1]{>{\RaggedRight\arraybackslash}p{#1}}
Mico
  • 506,678
  • @NilaniAlgiriyage - You're welcome! You provided a really good MWE (minimum working example), making it straightforward to provide a direct answer. – Mico Dec 03 '14 at 06:51
2

Mico beat me to it, so I'll post another option, the quick and dirty way to get the same result—maybe useful if this is the only table with this kind of spacing issue and you don't want to load another package.

You can just use \par to insert line breaks and imitate \raggedright:

\documentclass{article}

\begin{document} \begin{table}[h] \centering \begin{tabular}{|p{9.0cm}|p{1.5cm}|} \hline User-agent string & dummy\ \hline Googlebot (compatible; Googlebot/2.1; \par +http://www.google.com/bot.html)&10\ Googlebot-Mobile/2.1&15\ Googlebot/2.1 (+http://www.google.com/bot.html)&20\ Mozilla/5.0 (compatible; Googlebot/2.1;\par +http://www.google.com/bot.html)&40\ Mozilla/5.0 (compatible; Googlebot/2.1;\par+http://www.google.com/bot.html)&50\ \hline \end{tabular} \end{table} \end{document}

enter image description here

It gives the exact same result, but it's a bit of a hack—Mico's answer is the "correct" one.

Ryan
  • 2,914
  • 4
    TeX.SE feels like a race sometimes! I always hate deleting my answers after I see that little "one new answer, click to reload" icon. The community is too good. Thanks for a good question with a good MWE though! – Ryan Dec 03 '14 at 06:53