4

I'm using ~ to create indentation in a tabularx table. When space runs LaTeX take two lines which is fine but could be prettier.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document} \begin{tabularx}{0.3\textwidth}{@{}Xccc} \hline City & Number\ \hline \textbf{Group 1}\

~~~ Khatmandu & 2\\ % I'm aware of the typo but already pasted the screenshots
\textbf{Group 2}\\
~~~ Beijing & 14 \\
\hline
\end{tabularx}
\end{document}
</code></pre>
<p>The outcome is</p>
<p><a href="https://i.stack.imgur.com/uoGr1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uoGr1.png" alt="enter image description here" /></a></p>
<p>But I would rather have either of the following</p>
<p><a href="https://i.stack.imgur.com/yxejL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yxejL.png" alt="enter image description here" /></a></p>
<p><strong>EDIT</strong>: The tables are produced by an external script so the best solution would be fully automatable.</p>
<p>Any ideas appreciated.</p>
snoram
  • 240

5 Answers5

5

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document} \begin{tabularx}{0.3\textwidth}{@{}>{\leftskip1em\hspace{0pt}}Xccc} \hline \hspace{-1em}City & Number\ \hline \hspace{-1em}\textbf{Group 1}\ Delhi& 2 \ Khatmandu & 2\ % I'm aware of the typo but already pasted the screenshots \hspace{-1em}\textbf{Group 2}\ Beijing & 14 \ \hline \end{tabularx}

\end{document}

David Carlisle
  • 757,742
4

I'm pretty sure some manual intervention is required. In this case, I use a left-aligned \Longunderstack to accomplish the split.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx,stackengine}

\begin{document} \begin{tabularx}{0.3\textwidth}{@{}Xccc} \hline City & Number\ \hline \textbf{Group 1}\

~~~ \Longunderstack[l]{Kath- ~~mandu\strut} &amp; 2\\ 
\textbf{Group 2}\\
~~~ Beijing &amp; 14 \\
\hline
\end{tabularx}
\end{document}
</code></pre>
<p><a href="https://i.stack.imgur.com/2xxyp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2xxyp.jpg" alt="enter image description here" /></a></p>
2

What you want in table 1 of your sample list, is not achievable automatically unless you manually break the word and add kerns or skips as necessary or write a complex macro. It is not good typography. Your second one is bit better but still unsightly typography.

You can have text in different states a) justified b) centered c) ragged left and~d) ragged right or e) verbatim and possibly others.

enter image description here

A\fbox{\parbox{1cm}{\raggedleft Khatm\-andu}}B
\fbox{\parbox{1cm}{\raggedleft Kat\-man\-du}}C
\fbox{\parbox{1cm}{\raggedright Kat\-man\-du}}D
\fbox{\parbox{1cm}{\centering Kat\-man\-du}}E
\fbox{\parbox{1cm}{\justify Kat\-man\-du and}}F
\fbox{\parbox{1cm}{\language-1  Katmandu}}G
\fbox{\parbox{2cm}{%
Kat-\\
\hphantom{ZZ}man-\\
\hphantom{ZZZZZZ}du}}E

TeX/LaTeX is a teacher let it first tell you what it thinks, by not tweaking any parameters first and then try and improve or meet some constraints.

The p{} specifier denotes a parbox. That is why I used the parboxes to demonstrate the differences in glue specification.

    \documentclass{article} 
     \usepackage{ragged2e}
     \usepackage{microtype}
    \begin{document} 
    \begin{tabular}{p{1.45cm}|c}
    \hline
    City & Number\\
    \hline
    \textbf{Group 1}\\
     \raggedleft  Delhi& 2 \\
     \raggedleft photo\-gra\-phy in Khatmandu& 2\\ 
    \textbf{Group 2}\\
    \raggedleft Beijing & 14 \\
    \hline
    \end{tabular}
   \end{document}

You can modify the code in box H to suit and insert in your table if you insist to use one of your designs.

Try it with and without the package microtype to observe some subtle differences.

yannisl
  • 117,160
2

You can input “Groups” with \multicolumn in order to remove the default 1em space.

\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}

\begin{document}

\begin{tabularx}{0.3\textwidth}{@{\quad}>{\hspace{0pt}}Xccc} \hline \multicolumn{1}{@{}l}{City} & Number\ \hline \multicolumn{1}{@{}l}{\textbf{Group 1}}\ Delhi& 2 \ Khatmandu & 2\ % I'm aware of the typo but already pasted the screenshots \multicolumn{1}{@{}l}{\textbf{Group 2}}\ Beijing & 14 \ \hline \end{tabularx}

\end{document}

The >{\hspace{0pt}} bit is for allowing hyphenation in the first word.

enter image description here

Alternatively:

\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}

\begin{document}

\begin{tabularx}{0.3\textwidth}{@{}>{\hangindent=2em\hangafter=1\relax}Xccc} \hline City & Number\ \hline \textbf{Group 1}\ \quad Delhi& 2 \ \quad Khatmandu & 2\ % I'm aware of the typo but already pasted the screenshots \textbf{Group 2}\ \quad Beijing & 14 \ \hline \end{tabularx}

\end{document}

enter image description here

If you change \hangindent=2em into \hangindent=1em you get the same as before.

egreg
  • 1,121,712
1

With tabularray, use of three columns and manually inserted points of hyphenation Kathmandu:

\documentclass{article}
\usepackage{tabularray}

\begin{document} \begin{tblr}{width=0.3\linewidth, colspec={@{} l X[1,l] X[1,c] @{}}, hline{1,2,Z}=solid } \SetCell[c=2]{l} City & & Number\ \SetCell[c=2]{l} \textbf{Group 1}
& & \ & Delhi & 2 \ & Khat-mandu & 2 \ \SetCell[c=2]{l} \textbf{Group 2} & & \ & Benjing & 14 \ \end{tblr} \end{document}

enter image description here

Zarko
  • 296,517