Center only part of content in a table cell

From general point of view, it is very interesting problem (even though you probably don't need such general solution in your application).

What we need: a centered column in a table with sometimes an inteligent\rlap (\rl in my example) at the right side of items. This \rl behaves as normal \rlap when the items are centered (i.e. their width are ignored) but their width must not be ignored when next column of the table is created.

We cannot take the most big \rl data as relevant (it is done in another answer here) because this biggest \rl may be used in context of very small centered item. There are two situations:

|  CCxxxxxx|                          |  CCxxx | 
|CCCCCCxx  | ... first line wins      |CCCCCCxx|  ... second line wins

The problem is that we don't know which line wins when table items are typeset. This is known only after the last line of the table is created. So, we need to do some box calculation at the last line of the table. This is done by \fin macro in my example.

\newbox\boxA \newbox\boxB \newbox\boxC
\def\addbox#1#2{\global\setbox#1=\vbox{\unvbox#1\lastbox\box#2}}
\def\fin{\splittopskip=10pt plus 2pt \setbox0=\vsplit\boxA to0pt \setbox0=\vsplit\boxB to0pt
   \setbox\boxC=\vbox{}\dimen0=\wd\boxA
   \loop
      \setbox0=\vsplit\boxA to\baselineskip
      \dimen1=\dimen0 \advance\dimen1 by-\wd0 \divide\dimen1 by2
      \setbox\boxC=\vbox{\unvbox\boxC\hbox{\kern\dimen1\box0 \vsplit\boxB to\baselineskip}}%
      \unless\ifvoid\boxA \repeat
   \advance\dimen0 by-\wd\boxC
   \kern-\dimen0
}
\def\rl#1{\rlap{#1}\global\setbox\boxC=\hbox{#1}} 

\setbox\boxA=\vbox{\penalty-10000} \setbox\boxB=\vbox{\penalty-10000}   
\setbox\boxC=\hbox{}
\halign{#\unskip
   &\hfil\global\setbox\boxC=\hbox{}\setbox0=\hbox{#\unskip}\copy0\addbox\boxA0\hfil
   &\addbox\boxB\boxC#\unskip\hfil&#\unskip\cr
%
  L & Reference                              &&     R \cr
  L & CenterThisPart\rl{*ButNot}             &&     R \cr
  L & CenterThis\rl{*ButNotThisPartPartPart} &&     R \cr
  L & CenterThisP\rl{*ButNotThisPart}        &&     R \cr
  L & CenterThisPa\rl{*ButNotThisPart}       &\fin& R \cr
}
\bye

The L and R columns are here only in order to see that the total dimension of our column is calculated correctly. You can do experiments with this example (using pdftex test command, of course). For example, you can change the string lengths of centered or r-lapped parts in this example.

How it works: When the table is created then \rl behaves like \rlap but the centered part is copied to \boxA and the r-lapped part is copied to \boxB. At the last line and in the following empty column, we start the box calculation using \fin macro. The \boxA and \boxB are re-boxed in order to simulate the situation in centered column but with real dimensions of \rl parts. The result is saved to the \boxC. Finally, we do \kern d, where d is difference between the width of \boxA and \boxC.

EDIT Note, that the accepted solution (where 250 points are assigned) does not solve the original task. The simple \llap and \rlap is used here, no intelligent \llap, \rlap. You can see the bad effect when you try:

\documentclass{article}

\newcommand{\rhang}[1]{\makebox[0pt][r]{#1}}
\newcommand{\lhang}[1]{\makebox[0pt][l]{#1}}

\begin{document}
\begin{tabular}{c|c|c}
L&CenterThisPart\lhang{ButNotThisPart} & R \\
L&CenterThis\lhang{ButNotThis} & R \\
L&0654321 & R \\
L&321645789CenterThisPart\lhang{ButNotThisPart} & R \\
L&\rhang{thisthisThisPart}321645789 & R
\end{tabular}
\end{document}

Maybe something like this?

\documentclass{standalone}
\begin{document}

\newlength\mylength
\settowidth\mylength{ButNotThisPart}% longest "ButNotThisPart"

\begin{tabular}{c@{}p{\mylength}}
  Reference & \\
  CenterThisPart\makebox[0pt][l]{ButNotThisPart}\\
  CenterThisPart\makebox[0pt][l]{ButNot}\\
  Center\makebox[0pt][l]{ButNotThisPart}\\
  Center\makebox[0pt][l]{ButNot}\\
\end{tabular}

\end{document}

Note the & after Reference.

enter image description here


This would work for your example.

\documentclass{article}

\begin{document}
    \begin{tabular}{c}
        Reference \\
        \phantom{ButNotThisPart}CenterThisPartButNotThisPart
    \end{tabular}
\end{document}

enter image description here

But because you are using it with numbers I would agree with Zarkos solution and use the siunitx package to center the numbers. The documentation provides a lot of tabular examples but here is a really basic one with a little bit of explanation.

\documentclass{article}
\usepackage{siunitx}

\begin{document}
    \begin{tabular}{ | S[table-format=-3.1] | } % the column width is now set to fit a minus sign, three digits before and one after the comma and this type of number gets displayed centred. 
        -123.4 \\
        12 \\ % even though there is no decimal digit it stays nicely aligned
        1.345 \\ % Additional numbers will simply overflow the column.
        123.4{*} \\ % put the sign you want to use in {} the number will stay aligned correctly
        {text} % if you only write text in {} it will be treated like a c column
    \end{tabular}
\end{document}