5

After lots of calculation in Python, a Pandas DataFrame is created that has several vectors. Fortunately Pandas can export its table in booktabs format, but the "vectors" get aligned by LaTeX and I'd like to keep the same spacing/alignment as exported, i.e., each element of the vectors stays aligned in the column. I've found that \verb can be used element/cell-wise, but my table has many columns and even more rows. I tried several array \newcolumntype definitions with \verb but none were even close to rendering properly or just spewed errors. It seems like there should be a way to specify the desired format for the entire column.

Here's a MWE that has many fewer columns and rows than my actual data, but still shows the issue.

\documentclass{article}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{rrrrllr}
    \toprule
    ID & n & M   & B    & vector 1          & vector 2              & SC   \\
    \midrule
    -1 & 4 & 123 & 1475 & [ 36   1  43 113] & [ 574  253  636 1266] & 1519 \\
    -3 & 4 & 192 & 2111 & [ 54  74 147  30] & [ 736  913 1567  520] & 2169 \\
    -5 & 4 & 446 & 4125 & [ 68 226 238 155] & [ 855 2278 2389 1637] & 4026 \\
    \bottomrule
\end{tabular}
\end{document}

This gets rendered as:

enter image description here

How can I have the Vector columns retain their element alignment as shown in the LaTeX source?

Is there a way to center the column headers and right align the column data below it?

4 Answers 4

2

Like this?

enter image description here

One way is to put each matrix column in separate table column. For example with use of tabularray package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
\begin{tblr}{colspec={Q[r,$] ccc r@{\ }r@{\ }r@{\ }r r@{\ }r@{\ }r@{\ }r c}}
    \toprule
\SetCell[c=1]{c} \text{ID}
    & n & M & B      & \SetCell[c=4]{c} vector 1    
                            &     &    &    & \SetCell[c=4]{c} vector 2   
                                                      &      &      &       & SC   \\
    \midrule
-1 & 4 & 123 & 1475 & [ 36 &   1 &  43 & 113] & [ 574 &  253 &  636 & 1266] & 1519 \\
-3 & 4 & 192 & 2111 & [ 54 &  74 & 147 &  30] & [ 736 &  913 & 1567 &  520] & 2169 \\
-5 & 4 & 446 & 4125 & [ 68 & 226 & 238 & 155] & [ 855 & 2278 & 2389 & 1637] & 4026 \\
    \bottomrule
\end{tblr}
\end{document}

Of course, this can be also dona with tabular table:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{>{$}c<{$} ccc r@{\ }r@{\ }r@{\ }r r@{\ }r@{\ }r@{\ }r c}
    \toprule
\text{ID}
    & n & M & B      & \multicolumn{4}{c}{vector 1}  
                                              & \multicolumn{4}{c}{vector 2}   
                                                                            & SC   \\
    \midrule
-1 & 4 & 123 & 1475 & [ 36 &   1 &  43 & 113] & [ 574 &  253 &  636 & 1266] & 1519 \\
-3 & 4 & 192 & 2111 & [ 54 &  74 & 147 &  30] & [ 736 &  913 & 1567 &  520] & 2169 \\
-5 & 4 & 446 & 4125 & [ 68 & 226 & 238 & 155] & [ 855 & 2278 & 2389 & 1637] & 4026 \\
    \bottomrule
\end{tabular}
\end{document}
1
  • Well, you're lucky because the number of digits in the first entries match across the column.
    – egreg
    Oct 29, 2021 at 21:22
2

Something like this?

\documentclass{article}
\usepackage{booktabs}
\usepackage{mathtools}

\begin{document}

\[ \begin{array}{*{4}{r}*{2}{r@{\enspace}r@{\enspace}r@{\enspace}r}r}
    ID & n & M & B & \multicolumn{4}{c}{\text{vector 1}} & \multicolumn{4}{c}{\text{vector 2}} & SC \\
    \midrule
    -1 & 4 & 123 & 1475 & [\, 36 & 1 & 43 & 113\,] & [\, 574 & 253 & 636 & 1266\,] & 1519 \\[0.5ex]
    -3 & 4 & 192 & 2111 & [\, 54 & 74 & 147 & 30\,] & [\, 736 & 913 & 1567 & 520\,] & 2169 \\[0.5ex]
    -5 & 4 & 446 & 4125 & [\, 68 & 226 & 23 & 55\,] & [\, 855 & 2278 & 2389 & 1637\,] & 4026 \\
    \bottomrule
\end{array} \]

\end{document} 

enter image description here

2

I'm afraid you have to edit the table to insert some more & tokens.

Since we cannot be sure that all numbers the first entry of the vectors have the same digit length, we need to reserve a column for the open bracket; for symmetry and ease of input, I reserve a column also for the closed bracket.

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}

\begin{document}

\[
\begin{array}{
  @{}
  *{4}{r} % the first four columns
  c@{\,} % for the open bracket
  *{4}{r} % the first vector
  @{\,}c % for the closed brace
  c@{\,} % for the open bracket
  *{4}{r} % the second vector
  @{\,}c % for the closed brace
  r % the last column
  @{}
}
\toprule
% the headers
\multicolumn{1}{@{}c}{\text{ID}} &
\multicolumn{1}{c}{n} &
\multicolumn{1}{c}{M} &
\multicolumn{1}{c}{B} &
\multicolumn{6}{c}{\text{vector 1}} &
\multicolumn{6}{c}{\text{vector 2}} &
\multicolumn{1}{c@{}}{\text{SC}} \\
\midrule
-1 & 4 & 123 & 1475 & [ & 36 &   1 &  43 & 113 & ] & [ & 574 &  253 &  636 & 1266 & ] & 1519 \\
-3 & 4 & 192 & 2111 & [ & 54 &  74 & 147 &  30 & ] & [ & 736 &  913 & 1567 &  520 & ] & 2169 \\
-5 & 4 & 446 & 4125 & [ & 68 & 226 & 238 & 155 & ] & [ & 855 & 2278 & 2389 & 1637 & ] & 4026 \\
\bottomrule
\end{array}
\]

\end{document}

enter image description here

1
  • Your point about the number of digits is well taken. So, there really is no \verb that can act column-wise for a table?
    – rickhg12hs
    Oct 30, 2021 at 12:43
2

This approach may not be, in general, applicable, but works for the OP's situation. Advantages are: zero additional tabular formatting is required and columns 5 and 6 are center aligned but with right-aligned vectors, as requested by the OP.

This constraints under which this approach applies are also non-trivial: the [ token may only appear in the tabular to signify the start of a vector, and the column width for a given vector is defined by the widest element of that vector, without taking into account the column width for vectors in adjacent rows or columns. Such constraints may or may not be acceptable to the OP. If the latter constraint proves troublesome, there are some remedies, but which require a change to the input format to something other that hoped-for by the OP.

The method involves making [ active for the duration of the tabular and using features of tabstackengine to both make the vectors internally right aligned and with fixed-width columns.

\documentclass{article}
\usepackage{booktabs,tabstackengine}
\setstackTAB{ }
\fixTABwidth{T}
\setstacktabbedgap{1ex}% INTER-COLUMN GAP SIZE
\renewcommand\stackalignment{r}% ALIGNMENT WITHIN VECTOR
\begin{document}
\catcode`\[=\active
\def[#1]{\string[\Matrixstack{#1}\string]}
\begin{tabular}{rrrrccr}
    \toprule
    ID & n & M   & B    & vector 1          & vector 2              & SC   \\
    \midrule
    -1 & 4 & 123 & 1475 & [ 36   1  43 113] & [ 574  253  636 1266] & 1519 \\
    -3 & 4 & 192 & 2111 & [ 54  74 147  30] & [ 736  913 1567  520] & 2169 \\
    -5 & 4 & 446 & 4125 & [ 68 226 238 155] & [ 855 2278 2389 1637] & 4026 \\
    \bottomrule
\end{tabular}
\catcode`\[=12
\end{document}

enter image description here

You must log in to answer this question.

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