# [Tex/LaTex] tabular automatic p width and right alignment

tables

I am customizing a letter and I want to place a tabular with a multirow on the right side, here is a MWE:

\documentclass{article}
\begin{document}
\noindent
Text \\
\hrule
\hfill \begin{tabular}{r p{2cm}}
A & first line \newline second line \\
B & text
\end{tabular}
\end{document}


How can I set the column width of the p-column to the smallest possible value?
I tried \width but it messes up the tabular.

I have seen people suggesting to use tabularx, but then I would have to specify the width of the table itself, and hfill doesn't properly align it to the right anymore. But I want both the table and the p-column to be the smallest width possible and the table aligned to the right!

EDIT: Also, A should be aligned with the first line of the multi-row.

Personally I would suggest the following to produce the table.

\documentclass{article}
\usepackage{makecell}
\begin{document}
\hfill \begin{tabular}{r l}
A & \makecell[l]{first line\\ second line} \\
B & text
\end{tabular}
\end{document}


By using an l column the width is minimal (fits the length of the longest entry) and with the usage of makecell you can determine where linebreaks should occure.

As pointed out by Schweinebacke in the comments, one can replace \makecell[l] by \makecell[tl] to vertically align 'A' and 'first line'.