1

In this example

\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{document}
\begin{center}
    Apple street 16-214, Great City, Big Country\\
\noindent
  \begin{tabularx}{\streetlength}{Xr}
    Ph & 00 322 22299988\\
    E-mail & namesurname@emails.com
  \end{tabularx}
\end{center}
\end{document}

I try to set the width of the table equal to the length of the preceding line.

From the output

enter image description here

it is evident that the solution must be improved. How to adjust the solution?

2

If you add vertical lines left and right you will see that the width is exactly the wanted... If you want the letters to be aligned you have to remove the margins that produced by \tabcolsep length:

\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\begin{document}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{center}
    Apple street 16-214, Great City, Big Country\\
\noindent
  \begin{tabularx}{\streetlength}{|Xr|}
    Ph & 00 322 22299988\\
    E-mail & namesurname@emails.com
  \end{tabularx}
\end{center}

\setlength\tabcolsep{0pt}
\begin{center}
    Apple street 16-214, Great City, Big Country\\
\noindent
  \begin{tabularx}{\streetlength}{Xr}
    Ph & 00 322 22299988\\
    E-mail & namesurname@emails.com
  \end{tabularx}
\end{center}
\end{document}

Output:

enter image description here

  • 1
    I would suggest to use all the second case (from \setlength\tabcolsep{0}) to be included inside braces in case you want to change this length only for this table and not in all tables fallowing... – koleygr Jan 24 '18 at 1:55
3

You don't have to measure the length if you know the widest element:

enter image description here

\documentclass{article}

\begin{document}

\setlength{\tabcolsep}{0pt}
\begin{tabular}{ r l l }
  & Apple street 16-214, Great City, Big Country & \\
  \rlap{Ph} &             & \llap{00 322 22299988} \\
  \rlap{E-mail} &  & \llap{namesurname@emails.com}
\end{tabular}

\end{document}

We set the shorter lengths in additional columns left and right with an overlap into the middle natural-width l-column.

3

You could also use @{} before the first and after the last column.

\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{document}
\begin{center}
    Apple street 16-214, Great City, Big Country\\
\noindent
\begin{tabularx}{\streetlength}{@{}Xr@{}}
Ph & 00 322 22299988\\
E-mail & namesurname@emails.com
\end{tabularx}
\end{center}
\end{document}
  • why should one use @{}? – Viesturs Jan 24 '18 at 13:05
  • 1
    In that case you can avoid \setlength{\tabcolsep}{0pt}. – exilsteira Jan 24 '18 at 13:08

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.