0

I would like to format something using tabularx where the first row has a column starting at the left margin and one starting at the right, and the following rows being only one column spanning the entire text width allowing linebreaks in the text. Below is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{tabularx}

\begin{document}  
\section{Introduction}

\begin{flushleft}
    \begin{tabularx}{\textwidth}{@{}X @{\extracolsep{\fill}} r @{}}
         Text beginning at left margin & Something at right margin   \\
         \hline
          Here is some text that can get longer and longer until it causes a linebreak eventually & \\
         \textit{Some closing text that would go to the next line soon because of the cell to the right} & \\
    \end{tabularx}
\end{flushleft}


\begin{flushleft}
    \begin{tabularx}{\textwidth}{@{}X @{\extracolsep{\fill}} r @{}}
         Text beginning at left margin & Something at right margin  \\
         \hline
         \multicolumn{2}{p{\textwidth}}{Here is some longer sentence that goes past the original column on the right } \\
         \multicolumn{2}{p{\textwidth}}{\textit{Here is some text that can get longer and longer and longer and longer and longer until it causes a linebreak eventually}} \\
    \end{tabularx}
\end{flushleft}

\end{document}

In the picture below, the top part looks as expected, however, the bottom two rows do not have their columns merged. When I try to do it with the bottom code, I first notice an indentation of the 2nd and 3rd row. Does that come from the multicolumn command? How do I modify the indentation? Also, the text in these rows exceeds the right page margin when using \textwidth as the width of p column. If I a length that is equal top the paperwidth minus the margins, it works. What is going on here? Also, I am happy about alternative suggestions to format.enter image description here

0

You can use @{} in order to remove the small (6 pt) horizontal white space to the left and right of a table cell as you already did in your table code. Since the \multicolumn command overrides this setting, the padding is added back in, resulting in a p type column that is as wide as \textwidth+2\tabcolsep and this runs into the right margin. To overcome this, add @{} to your \multicolumn command as shown in the following example. I have also removed @{\extracolsep{\fill}} as this command is not needed here.

In the following screenshot, the red lines indicate the page margins:

enter image description here

\documentclass{article}
%\usepackage[utf8]{inputenc} % default in an up to date installation
%\usepackage{multicol} % Not related to the \multicolumn command
\usepackage{tabularx}

\begin{document}  
\section{Introduction}

\noindent
    \begin{tabularx}{\textwidth}{@{} X r @{}}
         Text beginning at left margin & Something at right margin  \\
         \hline
         \multicolumn{2}{@{}p{\textwidth}@{}}{Here is some longer sentence that goes past the original column on the right } \\
         \multicolumn{2}{@{}p{\textwidth}@{}}{\textit{Here is some text that can get longer and longer and longer and longer and longer until it causes a linebreak eventually}} \\
    \end{tabularx}

\end{document}
| improve this answer | |
0

From -- https://latex.org/forum/viewtopic.php?t=8352

enter image description here

\multicolumn{2}{@{}p{\textwidth}}{Here is some longer sentence that goes past the 
 original column on the right } \\
| improve this answer | |
  • Thank you, this removes the indentation. Do you have an idea why \textwidth makes the text exceed the right margin? – jon_tea Sep 10 at 10:22
  • please see the link in comments section above – js bibra Sep 10 at 10:25
  • I saw it now, thanks a lot! – jon_tea Sep 10 at 10:38

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.