6

I have a table where I want to center part of the content in a cell and just let the rest of the content adjust to this. Minimal (non-)working example:

\documentclass{standalone}
\begin{document}
\begin{tabular}{c}
Reference \\
CenterThisPartButNotThisPart
\end{tabular}
\end{document}

In this specific example, what I'd like to achieve is to have "CenterThisPart" being centered just like if it was the only content and let the rest of the text ("ButNotThisPart") just continue to the right.

To provide some context what I'm trying to do: I'm putting numbers in a table and some of them have asterisks to indicate significant p-values. I want all the numbers to line line up no matter if they have asterisks or not.

  • how you will know, when is something centered? see, if use two columns like \begin{tabular}{cl} Reference & \\ CenterThisPart & ButNotThisPart \end{tabular} solve your problem – Zarko May 2 '17 at 15:06
  • @Zarko To provide some context what I'm trying to do: I'm putting numbers in a table and some of them have asterisks to indicate significant p-values. I want all the numbers to line line up no matter if they have asterisks or not. Your answer would split the content of the cell into two distinct and separated places. – Speldosa May 2 '17 at 15:16
  • hm, now the question is more clear. see my answer – Zarko May 2 '17 at 15:30
2
+250

EDIT: @Wipet's answer is better constructed, and will work in a more complex case scenario. This solution does not take into account the content of adjacent cells.


What about just making an asterisk that takes no horizontal space?

Code:

\documentclass{article}

\newcommand{\rhangstar}{\makebox[0pt][r]{*}}
\newcommand{\lhangstar}{\makebox[0pt][l]{*}}

\begin{document}
\begin{tabular}{c}
1234567890\lhangstar \\
12347890\lhangstar \\
0654321 \\
321645789\lhangstar \\
\rhangstar321645789
\end{tabular}
\end{document}

Output:

enter image description here

This can easily be implemented as a macro to work with (I guess) any kind of text.

Code:

\documentclass{article}

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

\begin{document}
\begin{tabular}{c}
CenterThisPart\lhang{ButNotThisPart} \\
CenterThis\lhang{ButNotThis} \\
0654321 \\
321645789CenterThisPart\lhang{ButNotThisPart} \\
\rhang{ThisPart}321645789
\end{tabular}
\end{document}

Output: enter image description here

  • Could you please use the OP's example and in the best case show some output? – TeXnician May 14 '17 at 9:40
  • This is great! Is it possible to implement it to uncenter content in front of the centered content as well? – Speldosa May 14 '17 at 13:29
  • @Speldosa: See above. I edited the answer. And thank you! :) – El Andi May 14 '17 at 23:07
  • 1
    @ElAndi Excellent! This solution gives you the power to modify everything exactly as you want, and it works both in text and math mode. – Speldosa May 15 '17 at 10:41
  • @Speldosa This is only simple \llap and \rlap. It does not solve the original task. See the counterexample at the end of my answer. – wipet May 16 '17 at 5:33
5

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}
  • I admit this is a better solution, covering a wider case scenario. – El Andi May 16 '17 at 19:23
4

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

  • c@{}p{\mylength}. Otherwise you will have 2\tabcolsep too much space on the right, – Henri Menke May 12 '17 at 1:10
  • @Henri Menke: You are right, thank you. I have edited. – Raoul Kessels May 12 '17 at 6:20
3

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}
3

You can enclose the asterisk using \lefteqn which will give it zero width.

\documentclass{article}
\begin{document}
\begin{tabular}{c}
Reference \\
.1234\lefteqn{{}^*}\\
.1234\\
CenterThisPart\lefteqn{\mbox{ButNotThisPart}}
\end{tabular}
\end{document}

Is this what you had in mind?

enter image description here

2

Based on your description, it seems that you can use \rlap to ignore a portion of the text:

CenterThisPart\rlap{ButNotThisPart}

which yields

enter image description here

Notes:

  • This however does require that some other row sets the width of the column:

Code:

\documentclass{article}
\begin{document}
\begin{tabular}{|c|}
  Reference \\
  CenterThisPart\rlap{ButNotThisPart} \\
  wide row to ensure column width is appropriate \\
\end{tabular}
\end{document}

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.