0

When I make the table \columnwidth, everything shifts to the left.

\begin{table}
    \caption{caption}
    \begin{tabularx}{\columnwidth}{|c||c|c|c|c|}
    \hline
    & 1 & 2 & 3 & 4 \\
    \hline
    A & B & C & D & E \\
    \hline
    \end{tabularx}
\end{table}

How can I center it in the simplest way, without using absolute width?

enter image description here

1 Answers1

1

As David has said, using a tabular environment can put contents at the center.

Since you are already using tabularx, I assume you want the contents to fill the column width, so it should be based on the X column type, but with a little modification to make it centered (as is discussed here)

\newcolumntype{Y}{>{\centering\arraybackslash}X} % centered X

Therefore, it is

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document} \begin{table} \caption{caption} \begin{tabularx}{\columnwidth}{|Y||Y|Y|Y|Y|} \hline & 1 & 2 & 3 & 4 \ \hline A & B & C & D & E \ \hline \end{tabularx} \end{table} \end{document}

Result

By the way, when asking a question, it would be better if you provide a full minimum working example (MWE) from \documentclass to \end{document}.