General\newline (or equivalent) inside a \caption

LaTeX specific issues not fitting into one of the other forums of this category.
ahallam
Posts: 4
Joined: Thu Aug 20, 2009 11:51 pm

Re: \newline (or equivalent) inside a \caption

Post by ahallam »

Any idea about margins and the use of caption.

Consider the following two files.

\documentclass[12pt]{article}
\usepackage[font=sl,labelfont=bf]{caption}

\begin{document}

\begin{table}[!ht]
\centering
\caption[]{\bfseries Iowa State University\\College of Liberal Arts \& Sciences \\Replacement Costs}\label{Ta:ISUReplace}
\begin{tabular}{|l|l|} \hline
\bfseries Department & \bfseries Typical Replacement Cost \\ \hline
Anthropology & \$6,000.00 \\ \hline
BBMB & \$10,000.00 \\ \hline
\end{tabular}
\end{table}

The first does not center the caption while the second does.

\usepackage[lmargin=0.5in, rmargin=0.5in, vmargin=1.0in]{geometry}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

ahallam wrote:Any idea about margins and the use of caption.

Consider the following two files.

\documentclass[12pt]{article}
\usepackage[font=sl,labelfont=bf]{caption}

\begin{document}

\begin{table}[!ht]
\centering
\caption[]{\bfseries Iowa State University\\College of Liberal Arts \& Sciences \\Replacement Costs}\label{Ta:ISUReplace}
\begin{tabular}{|l|l|} \hline
\bfseries Department & \bfseries Typical Replacement Cost \\ \hline
Anthropology & \$6,000.00 \\ \hline
BBMB & \$10,000.00 \\ \hline
\end{tabular}
\end{table}

The first does not center the caption while the second does.

\usepackage[lmargin=0.5in, rmargin=0.5in, vmargin=1.0in]{geometry}
I only see a snippet of one file...
1,1,2,3,5,8,13,21,34,55,89,144,233,...
ahallam
Posts: 4
Joined: Thu Aug 20, 2009 11:51 pm

Re: \newline (or equivalent) inside a \caption

Post by ahallam »

If you add this line to the file I posted, then the caption appears correctly.

\usepackage[lmargin=0.5in, rmargin=0.5in, vmargin=1.0in]{geometry}


\documentclass[12pt]{article}
\usepackage[font=sl,labelfont=bf]{caption}

\begin{document}

\begin{table}[!ht]
\centering
\caption[]{\bfseries Iowa State University\\College of Liberal Arts \& Sciences \\Replacement Costs}\label{Ta:ISUReplace}
\begin{tabular}{|l|l|} \hline
\bfseries Department & \bfseries Typical Replacement Cost \\ \hline
Anthropology & \$6,000.00 \\ \hline
BBMB & \$10,000.00 \\ \hline
\end{tabular}
\end{table}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

ahallam wrote:If you add this line to the file I posted, then the caption appears correctly...
Well, that depends on what you mean by "correctly". The standard LaTeX document classes (article, report, and book) automatically center a caption if it fits in one single line, but treat it as a normal text paragraph if it occupies two or more lines (and the caption package adopts this behaviour).

In your case, the caption will be treated as a paragraph with three manually created lines, since you used the \\ command. If you want to center the text, you can use the justification=centering option.

In the example below I included that option locally, so that it will only affect the current float (but, of course, you can set that option globally).

Please refer to the package documentation for further information.

Code: Select all

\documentclass[12pt]{article}
\usepackage[font=sl,labelfont=bf]{caption}

\begin{document}

\begin{table}[!ht]
  \centering
  \captionsetup{font=bf,justification=centering}
  \caption{Iowa State University\\College of Liberal Arts \& Sciences \\Replacement Costs}\label{Ta:ISUReplace}
  \begin{tabular}{|l|l|} \hline
    \bfseries Department & \bfseries Typical Replacement Cost \\ \hline
    Anthropology & \$6,000.00 \\ \hline
    BBMB & \$10,000.00 \\ \hline
  \end{tabular}
\end{table}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

\newline (or equivalent) inside a \caption

Post by sommerfee »

thrillRseekR wrote:and that \newline does not always perform the same function as \\ does (or at least I don't think it did for me).
This is not a problem of the caption package but a "feature" of LaTeX instead:

Code: Select all

\documentclass{article}
\begin{document}
\noindent\parbox\linewidth{\centering one\\two three}
\par\bigskip
\noindent\parbox\linewidth{\centering one\newline two three}
\end{document}
As you see \newline does not behave very well in combination with \centering.
ahallam
Posts: 4
Joined: Thu Aug 20, 2009 11:51 pm

\newline (or equivalent) inside a \caption

Post by ahallam »

gmedina wrote:
ahallam wrote:If you add this line to the file I posted, then the caption appears correctly...
Well, that depends on what you mean by "correctly". The standard LaTeX document classes (article, report, and book) automatically center a caption if it fits in one single line, but treat it as a normal text paragraph if it occupies two or more lines (and the caption package adopts this behaviour).

In your case, the caption will be treated as a paragraph with three manually created lines, since you used the \\ command. If you want to center the text, you can use the justification=centering option.

In the example below I included that option locally, so that it will only affect the current float (but, of course, you can set that option globally).

Please refer to the package documentation for further information.

Code: Select all

\documentclass[12pt]{article}
\usepackage[font=sl,labelfont=bf]{caption}

\begin{document}

\begin{table}[!ht]
  \centering
  \captionsetup{font=bf,justification=centering}
  \caption{Iowa State University\\College of Liberal Arts \& Sciences \\Replacement Costs}\label{Ta:ISUReplace}
  \begin{tabular}{|l|l|} \hline
    \bfseries Department & \bfseries Typical Replacement Cost \\ \hline
    Anthropology & \$6,000.00 \\ \hline
    BBMB & \$10,000.00 \\ \hline
  \end{tabular}
\end{table}

\end{document}

If I run LaTeX on the file you suggest, I get the following errors.

! Use of \caption@@settype doesn't match its definition.
\@ifnextchar ... \reserved@d =#1\def \reserved@a {
#2}\def \reserved@b {#3}\f...
l.9 ...beral Arts \& Sciences \\Replacement Costs}
\label{Ta:ISUReplace}

! Argument of \@firstoftwo has an extra }.
<inserted text>
\par
l.9 ...beral Arts \& Sciences \\Replacement Costs}
\label{Ta:ISUReplace}


I also updated the caption package to 2008/08/24 v3.1j.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

\newline (or equivalent) inside a \caption

Post by sommerfee »

ahallam wrote:If I run LaTeX on the file you suggest, I get the following errors.
The file runs fine here, using caption package 3.1j. And I get exactly the same errors as you when using the caption package 3.0k instead. So I assume that you use the caption package 3.0k or a similar version, but not the actual version 3.1.
I also updated the caption package to 2008/08/24 v3.1j.
But are you sure it's actually used? Please add a \listfiles to the very top of the document and process it again. Afterwards you should see the following lines in the log file:

Code: Select all

 *File List*
 article.cls    2005/09/16 v1.4f Standard LaTeX document class
  size12.clo    2005/09/16 v1.4f Standard LaTeX file (size option)
 caption.sty    2008/08/24 v3.1j Customizing captions (AR)
caption3.sty    2008/08/24 v3.1j caption3 kernel (AR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
 ***********
(The feature that \\ will be replaced by a space in list entries was introduced in version 3.1 of the caption package.)
ahallam
Posts: 4
Joined: Thu Aug 20, 2009 11:51 pm

Re: \newline (or equivalent) inside a \caption

Post by ahallam »

Something was strange with texhash. The only copy I had of caption.sty was 3.1 but I still saw 3.0 in list file. I recopied the other sty files associated with caption.sty to the directory and reran texhash and not everything is great. Thanks.
Post Reply