0

How can I align my text in the table center then align it with the letter "M" ? Like the below it is right aligned but I want it center-right aligned. How ?

enter image description here

Like below, the text is center aligned with M.

enter image description here

4
  • hard to say without a MWE. And I actually don't understand what you're asking Commented Apr 29, 2020 at 15:00
  • What does the M stand for?
    – leandriis
    Commented Apr 29, 2020 at 15:16
  • @AlessandroCuttin I have updated my post
    – Kong
    Commented Apr 29, 2020 at 15:36
  • what is "center-right aligned", btw? Commented Apr 29, 2020 at 16:13

2 Answers 2

3

Assuming that is the first image the one you want to replicate, here's a MWE. You can override the column alignment for a given cell by using \multicolumn for a single column (see also the last line - I added it to make it more clear).

In the first image you posted, the first cell from the top seems to be centered. This is obtained with \multicolumn{1}{|c|}{\# Params}. The remaining ones appear to be simply right-aligned. Since the content of every cell ends with an M (millions?), everything looks like to be aligned with the M.

\documentclass{standalone}
\begin{document}
\begin{tabular}{|r|}
    \hline
    \multicolumn{1}{|c|}{\# Params} \\
    \hline
    2.8M\\
    15.4M\\
    38.7M\\
    \hline
    2.9M\\
    62M\\
    12.9M\\
    \hline
    \multicolumn{1}{|c|}{x} \\ % to prove that you can override the column alignment
\end{tabular}
\end{document}

enter image description here

A better looking result can be achieved with the help of siunitx, assuming that M is a unit or a Mega prefix. The package allows to define the S and s type of column. You can play with the unit alignment with the table-unit-alignment key, that accepts values such as left, right' orcenter`.

\documentclass{standalone}
\usepackage{siunitx}
\begin{document}
\begin{tabular}
{|S[table-format=2.1]@{\,}s[table-unit-alignment = center]|}
    \hline
    \multicolumn{2}{|c|}{Parameters} \\
    \hline
    2.8  & \mega \\
    15.4 & \mega \\
    38.7 & \mega \\ \hline
    2.9  & \mega \\
    62   & \mega \\
    12.9 & \mega \\
    \hline
\end{tabular}
\end{document}

enter image description here

3
  • Thank you ! But what if I want to shift the entries with M slightly to the left and still have them aligned? Do I manually use \ ?
    – Kong
    Commented Apr 29, 2020 at 16:25
  • see the edit I just made. Basically the length of the word Parameters is driving the width of the table, and -therefore- how the cell contents look when aligned according to the column definition Commented Apr 29, 2020 at 16:31
  • 1
    Thank you very much !
    – Kong
    Commented Apr 29, 2020 at 17:05
4

I'm not sure that I've understood your objective correctly, as the layouts in the two screenshots you've posted seem to be in direct conflict with each other. I've interpreted it as follows: (a) the numbers should be aligned on their decimal markers, (b) the M letters should be aligned, and (c) the number/M combinations should be centered horizontally within the column.

If this interpretation is correct, the following solution, which employs the S column type provided by the siunitx package, may be of relevance.

enter image description here

\documentclass{article}
\usepackage{siunitx}
\begin{document}
\begin{table}
\begin{tabular}{|S[table-format=3.1,table-space-text-post={M}]|}
\hline
{\# Params}\\
\hline
15.4M\\ 2.8M\\ 388.7M\\
\hline
\end{tabular}
\end{table}
\end{document}
2
  • 1
    I'd accept this answer rather than the mine, myself! :) (+1) Commented Apr 29, 2020 at 16:41
  • 1
    thank you that looks easier to interpret
    – Kong
    Commented Apr 29, 2020 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .