# Same text alignment inside a parbox as the environment where it is

How \parbox could "inherit" the "alignment mode" used in the environment where it is called? In other words, how can I automate the text alignment setting into a \parbox, in order to take the same text-alignment as the main paragraph?

I am thinking about three if/then sentences for left, right or center "modes", but I don't know how "get" the "current" value that latex is using at that point. I mean, something like that:

\begin{center}
Some text here
\if@... \parbox{0.5\textwidth}{\centering Some text here}
\if@... \parbox{0.5\textwidth}{\raggedright ...}
\if@... \parbox{0.5\textwidth}{\raggedleft ...}
Some text here
\end{center}


I would like it works under both environments (\begin{center}...) and declarations (\centering)

Looking at the \meaning of the various (4) modes, one sees that the 2 lengths \parfillskip and \rightskip, in combination, form a unique identifier for each mode. Therefore, just use the expressions of these two lengths, saved in a string, to make the identification by way of \ifx comparison.

I then introduce \justparbox to be the vehicle by which the justification of the surrounding environment is examined and set for the \parbox.

The limitation would be that it can't be used where \rightskip is otherwise altered, for example, inside some list environments.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\xdef\justcase{\the\parfillskip,\the\rightskip}
{\raggedright\xdef\ragrcase{\the\parfillskip,\the\rightskip}}
{\raggedleft\xdef\raglcase{\the\parfillskip,\the\rightskip}}
{\centering\xdef\cencase{\the\parfillskip,\the\rightskip}}
\newcommand\justparbox[3][c]{\xdef\tmp{\the\parfillskip,\the\rightskip}%
\ifx\tmp\ragrcase\parbox[#1]{#2}{\raggedright#3}\else
\ifx\tmp\raglcase\parbox[#1]{#2}{\raggedleft#3}\else
\ifx\tmp\cencase\parbox[#1]{#2}{\centering#3}\else
\parbox[#1]{#2}{#3}\fi\fi\fi}
\begin{document}
\meaning\centering

\meaning\raggedright

\meaning\raggedleft

\hrulefill

\newcommand\mydata{\lipsum[4]\par\justparbox[t]{5in}{\lipsum[4]}%
\par\noindent\hrulefill\par}

\mydata
{\raggedright\mydata}
{\raggedleft\mydata}
{\centering\mydata}
\end{document}


• So far it worked fine within a tabular environment. However, I just found out that it doesn't within tabularx. To fix it, the \leftskip length must be used instead of \parfillskip. Jun 2 '20 at 16:29
• @e_moro Thanks for that updated information. Jun 2 '20 at 21:38

TeX has no \ifskip comparator, but we can emulate it.

\documentclass{article}
\usepackage{pdftexcmds}

\usepackage{lipsum}

\makeatletter
\newcommand{\equalskipTF}[2]{%
\ifnum\pdf@strcmp{\the\glueexpr#1\relax}{\the\glueexpr#2}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\newcommand{\testrlcenrr}{%
\equalskipTF{\rightskip}{\@flushglue}%
{% either center or raggedright
\equalskipTF{\leftskip}{\@flushglue}%
{% center
\let\rl@cen@rr\centering
}%
{% raggedright
\let\rl@cen@rr\raggedright
}%
}%
{% either raggedleft or justified
\equalskipTF{\leftskip}{\@flushglue}
{% raggedleft
\let\rl@cen@rr\raggedleft
}
{% justified
\let\rl@cen@rr\relax
}%
}%
}
\newcommand{\pparbox}[3][c]{%
\testrlcenrr
\parbox[#1]{#2}{\rl@cen@rr#3}%
}

\begin{document}

\section{Normal text}

\lipsum[2][1-3]

\bigskip

\noindent
\pparbox{10cm}{\lipsum[1][1-3]}

\section{Ragged right}

\begin{flushleft}
\lipsum[2][1-3]

\bigskip

\pparbox{10cm}{\lipsum[1][1-3]}
\end{flushleft}

\section{Center}

\begin{center}
\lipsum[2][1-3]

\bigskip

\pparbox{10cm}{\lipsum[1][1-3]}
\end{center}

\section{Ragged left}

\begin{flushright}
\lipsum[2][1-3]

\bigskip

\pparbox{10cm}{\lipsum[1][1-3]}
\end{flushright}

\end{document}


Explanation.

1. \centering has both \leftskip and \rightskip set to \@flushglue
2. \raggedright has zero leftskip and \rightskip=\@flushglue
3. \raggedleft has zero rightskip and \leftskip=\@flushglue
• The same comment as the another answer: It works fine into a tabular environment. And also a clear explanation. I would like to select both answers. I simply choose the first one. Oct 4 '19 at 22:57