2

I'm making a table with code:

\begin{table}[h]
\centering
\begin{tabular}{ | P{1.9cm} || P{1.5cm} | P{1.4cm} | P{2.9cm} | P{2.9cm} | }
\hline
Compression & Bulk Dim & Top Dim & Box Area (\AA^2) & Cell Area (\AA^2)\\
\hline
 0\%        & 3  & 3  & 156.195853232816 & 156.195853232816\\
 1\%        & 99 & 100& 170097.284170537 & 173550.948036462\\
 2\%        & 49 & 50 & 41669.5826235546 & 43387.7370091156\\
 3\%        & 97 & 100& 163294.087007508 & 173550.948036462\\
 4\%        & 24 & 25 & 9996.53460690024 & 10846.9342522789\\
 5\%        & 19 & 20 & 6265.18922411630 & 6942.03792145850\\
 4\% doubled& 48 & 50 & 39986.1384276010 & 43387.7370091156\\
 5\% doubled& 38 & 40 & 25060.7568964652 & 27768.1516858340\\
 \hline
\end{tabular}
\caption{Initial conditions of each simulation, dimensions given in units of 
number of unit cells}
\label{table:1}
\end{table}

and preamble:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[rightcaption]{sidecap}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\graphicspath{ {images/} }

My output should be nice and pretty but for some reason the column widths are inconsistent. Notice how the two right-most columns are the same width, but in the output, for only the row with the column headers, it makes the "Box Area (\AA^2)" cell less than 2.9cm wide so that the line separating this cell from "Cell Area (\AA^2)" does not line up with the rest of the column, and then to the right of "Cell Area (\AA^2)" the bar at the edge of the table is at the correct width but shifted from the edge of the table.

Schematic shown below:

| ____________ || __________ | ___________| _____________|_______________|____

| ____________ || __________ | ___________| _________________|_______________|

| ____________ || __________ | ___________| _________________|_______________|

| ____________ || __________ | ___________| _________________|_______________|

| ____________ || __________ | ___________| _________________|_______________|

| ____________ || __________ | ___________| _________________|_______________|

| ____________ || __________ | ___________| _________________|_______________|

For just the one cell, latex seems to be ignoring the column width of 2.9cm. Could someone explain why this is happening and offer a solution?

1

Lesson to be learned: Don't use any .pdf file as long as there are error messages during compilation

You need math mode for $^2$:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[rightcaption]{sidecap}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\graphicspath{ {images/} }


\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{ | P{1.9cm} || P{1.5cm} | P{1.4cm} | P{2.9cm} | P{2.9cm} | }
\hline
Compression & Bulk Dim & Top Dim & Box Area (\AA$^2$) & Cell Area (\AA$^2$)\\
\hline
 0\%        & 3  & 3  & 156.195853232816 & 156.195853232816\\
 1\%        & 99 & 100& 170097.284170537 & 173550.948036462\\
 2\%        & 49 & 50 & 41669.5826235546 & 43387.7370091156\\
 3\%        & 97 & 100& 163294.087007508 & 173550.948036462\\
 4\%        & 24 & 25 & 9996.53460690024 & 10846.9342522789\\
 5\%        & 19 & 20 & 6265.18922411630 & 6942.03792145850\\
 4\% doubled& 48 & 50 & 39986.1384276010 & 43387.7370091156\\
 5\% doubled& 38 & 40 & 25060.7568964652 & 27768.1516858340\\
 \hline
\end{tabular}
\caption{Initial conditions of each simulation, dimensions given in units of 
number of unit cells}
\label{table:1}
\end{table}

\end{document}
  • Haha I'm annoyed that the answer is so simple but thanks for responding so quickly – Eric Jun 16 '17 at 20:15
  • @Eric The error message could have given some clue, it starts with ! Missing $ inserted. :) – user36296 Jun 16 '17 at 20:18
1

This table I would design as follows:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[rightcaption]{sidecap}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{array, booktabs, makecell}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}% notused
\graphicspath{ {images/} }
\usepackage{siunitx}% added

%---------------------------------------------------------------%
\usepackage{showframe} % to show page layout, in real document had to be remove
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
    \centering
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} c 
                            *{2}{S[table-format=3.0]} 
                            *{2}{S[table-format=6.12]} @{}
                            }
    \toprule
            & \multicolumn{2}{c}{Dim}       &   \multicolumn{2}{c}{Area (\AA$^2$)}  \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-5}
Compression &   Bulk    &   Top             &   {Box}   & {Cell}                    \\
    \midrule
 0\%        & 3  & 3    & 156.195853232816 & 156.195853232816\\
 1\%        & 99 & 100  & 170097.284170537 & 173550.948036462\\
    \addlinespace[3pt]
 2\%        & 49 & 50   & 41669.5826235546 & 43387.7370091156\\
 3\%        & 97 & 100  & 163294.087007508 & 173550.948036462\\
    \addlinespace[3pt]
 4\%        & 24 & 25   & 9996.53460690024 & 10846.9342522789\\
 5\%        & 19 & 20   & 6265.18922411630 & 6942.03792145850\\
    \addlinespace[3pt]
 4\% doubled& 48 & 50   & 39986.1384276010 & 43387.7370091156\\
 5\% doubled& 38 & 40   & 25060.7568964652 & 27768.1516858340\\
    \bottomrule
\end{tabular*}
\caption{Initial conditions of each simulation, dimensions given in units of
number of unit cells}
\label{table:1}
    \end{table}
\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.