0

I've problems with the hfill command inside an X column of a tabularx instance.

I'd like to have 2 text elements in a cell, one left- and the other right-aligned.

However, when the 1st text element is too long, tabularx breaks the 2nd text element instead of doing a CR and then pushing the 2nd elemnt at the end of the new line.

Here is a MWE with the problem. I'd like to have the Some Company 2 element right-aligned, on a second line.

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{hyperref}
\usepackage{booktabs}

\usepackage{array,tabularx,cellspace,makecell}

\begin{document}



\begin{center}
    \begin{tabularx}{\linewidth}{lX}
        \hline
        First Name & Text1 \hfill\textbf{Some Company} \\
        &  email.com \\
        &  other info \\ \hline
        First Name & Text1 a awful lot longer that will nearly trigger a Carriage Return \hfill{  \textbf{Some Company 2}} \\ 
        &  email.com \\
          &  other info
        \\ \hline   
    \end{tabularx}
\end{center}

\end{document}

Here is the result MWE output

2
  • Please note that \hfill does not take an argument, all it does is inserting some space which can stretch infinitely. If you want to suppress line breaks around "Some Company 2", you should use an \mbox like so: \mbox{\textbf{Some Company 2}}. But the real question is: Why don't you use two columns for this?
    – Skillmon
    Jan 6, 2020 at 8:00
  • note that this is unrelated to tabularx it is just the standard behaviour that glue is dropped at a linebreak. Jan 6, 2020 at 8:58

1 Answer 1

3

How about a third, r type column?

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{tabularx}
\begin{document}

%\begin{center}
    \begin{tabularx}{\linewidth}{lX>{\bfseries\arraybackslash}r}
        \hline
        First Name & Text1 &\textbf{Some Company} \\
        &  email.com \\
        &  other info \\ \hline
        First Name & Text1 a awful lot longer that will nearly trigger a Carriage Return &  Some Company 2 \\ 
        &  email.com \\
          &  other info
        \\ \hline   
    \end{tabularx}
%\end{center}

\end{document}

enter image description here

1
  • I hadn't though about it, it will do the trick. :)
    – 3isenHeim
    Jan 6, 2020 at 8:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .