General\newline (or equivalent) inside a \caption

LaTeX specific issues not fitting into one of the other forums of this category.
thrillRseekR
Posts: 2
Joined: Mon Nov 17, 2008 3:34 am

\newline (or equivalent) inside a \caption

Post by thrillRseekR »

Hello,

I have done numerous searches on this forum for "inserting a new line in a caption" to no avail as too many terms are filtered out! I've tried both \\ and \newline but these do not seem to help. I have read the documentation for the package caption as well and can't seem to figure this. Any help would be greatly appreciated.

Thanks!

Recommended reading 2024:

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

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

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

\newline (or equivalent) inside a \caption

Post by phi »

Hello,

if your captions are longer than one line (without the line break), they will be set as a paragraph and you can use \\ freely. Otherwise, you have to stop LaTeX from centering short captions; how to do that depends on the document class you're using. If you want to use line breaks in captions, don't forget to specify a (possibly empty) short version, otherwise errors will occur.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

Hi,

you can use justified as the justification option of the caption package, as the following simple example illustrates:

Code: Select all

\documentclass{article}
\usepackage{caption}
\captionsetup{justification=justified}
\usepackage{lipsum}%just to generate some text

\begin{document}

\begin{table}[!ht]
  \centering
  \begin{tabular}{cc}\hline
   some text & some text\\
   some text & some text\\\hline
  \end{tabular}
  \caption{A table with line changes\\ inside the caption.\\\lipsum[1]}
  \label{tab:test}
\end{table}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

\newline (or equivalent) inside a \caption

Post by phi »

gmedina wrote:you can use justified as the justification option of the caption package, as the following simple example illustrates:
What exactly is the difference? I've just noticed that the caption package always allows line breaks inside captions, regardless of the chosen options—and "justified" is the default value for "justification". So it should be sufficient to include the caption package and give a short caption (without short caption, it never works, unless you \protect the line break):

Code: Select all

\documentclass{article}
\usepackage{caption}

\begin{document}

\begin{table}[!ht]
  \centering
  \begin{tabular}{cc}\hline
   some text & some text\\
   some text & some text\\\hline
  \end{tabular}
  \caption[]{A table with line changes\\inside the caption.}
  \label{tab:test}
\end{table}

\end{document}
Last edited by phi on Mon Nov 17, 2008 11:12 am, edited 1 time in total.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

phi wrote:...So it should be sufficient to include the caption package and give a short caption (without short caption, it never works, unless you \protect the line break)
Yes, phi, you are right regarding the first part: it is enough to load the caption package: However, line breaks cause no problems even without protecting the line break or without using an optional argument:

Code: Select all

\documentclass{article}
\usepackage{caption}

\begin{document}
\listoftables

\begin{table}[!ht]
  \centering
  \begin{tabular}{cc}\hline
   some text & some text\\
   some text & some text\\\hline
  \end{tabular}
  \caption{A table with line changes\\inside the caption\\and no optional argument nor \texttt{protect}}
  \label{tab:test}
\end{table}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

\newline (or equivalent) inside a \caption

Post by phi »

gmedina wrote:However, line breaks cause no problems even without protecting the line break or without using an optional argument:
Your code produces dozens of errors when I try to compile it. Perhaps we have different versions of the class and packages? The beginning of my log file reads:

Code: Select all

This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6) (format=pdflatex 2008.10.30)  17 NOV 2008 11:23
entering extended mode
 %&-line parsing enabled.
**\nonstopmode \input{"/tmp/rr.tex"}
(/tmp/rr.tex (/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo
File: size10.clo 2005/09/16 v1.4f Standard LaTeX file (size option)
)
\c@part=\count79
\c@section=\count80
\c@subsection=\count81
\c@subsubsection=\count82
\c@paragraph=\count83
\c@subparagraph=\count84
\c@figure=\count85
\c@table=\count86
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
)
(/usr/share/texmf-texlive/tex/latex/caption/caption.sty
Package: caption 2007/01/07 v3.0k Customising captions (AR)

(/usr/share/texmf-texlive/tex/latex/caption/caption3.sty
Package: caption3 2007/01/07 v3.0k caption3 kernel (AR)

(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty
Package: keyval 1999/03/16 v1.13 key=value parser (DPC)
\KV@toks@=\toks14
)
\captionmargin=\dimen103
\captionmarginx=\dimen104
\captionwidth=\dimen105
\captionindent=\dimen106
\captionparindent=\dimen107
\captionhangindent=\dimen108
))
No file rr.aux.
\openout1 = `rr.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
(/usr/share/texmf-texlive/tex/latex/ragged2e/ragged2e.sty
Package: ragged2e 2003/03/25 v2.04 ragged2e Package (MS)

(/usr/share/texmf-texlive/tex/latex/everysel/everysel.sty
Package: everysel 1999/06/08 v1.03 EverySelectfont Package (MS)
LaTeX Info: Redefining \selectfont on input line 125.
)
\CenteringLeftskip=\skip43
\RaggedLeftLeftskip=\skip44
\RaggedRightLeftskip=\skip45
\CenteringRightskip=\skip46
\RaggedLeftRightskip=\skip47
\RaggedRightRightskip=\skip48
\CenteringParfillskip=\skip49
\RaggedLeftParfillskip=\skip50
\RaggedRightParfillskip=\skip51
\JustifyingParfillskip=\skip52
\CenteringParindent=\skip53
\RaggedLeftParindent=\skip54
\RaggedRightParindent=\skip55
\JustifyingParindent=\skip56
)
No file rr.lot.
\tf@lot=\write3
\openout3 = `rr.lot'.

LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 9.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 9.
! Use of \caption@@settype doesn't match its definition.
\@ifnextchar ... \reserved@d =#1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...
l.13 ...no optional argument nor \texttt{protect}}
                                                  
If you say, e.g., `\def\a1{...}', then you must always
put `1' after `\a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.

! Argument of \@firstoftwo has an extra }.
<inserted text> 
                \par 
l.13 ...no optional argument nor \texttt{protect}}
                                                  
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.
The two errors are repeated several times until LaTeX gives up.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

Hi,
phi wrote:Your code produces dozens of errors when I try to compile it. Perhaps we have different versions of the class and packages?...


You are using and old version of the caption package and that causes those errors. I would suggest you to update that package. The following excerpt from your log file shows that you use caption3 2007/01/07 version 3.0k:
phi wrote: Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo

File: size10.clo 2005/09/16 v1.4f Standard LaTeX file (size option)
(/usr/share/texmf-texlive/tex/latex/caption/caption.sty

Package: caption 2007/01/07 v3.0k Customising captions (AR)
(/usr/share/texmf-texlive/tex/latex/caption/caption3.sty
Package: caption3 2007/01/07 v3.0k caption3 kernel (AR)

(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty
Package: keyval 1999/03/16 v1.13 key=value parser (DPC)
\KV@toks@=\toks14
Here's the relevant part of the log file I got after compiling my example code, adding \listilesAs to get a list of the packages used and their versions:

Code: Select all

 *File List*
 article.cls    2005/09/16 v1.4f Standard LaTeX document class
  size10.clo    2005/09/16 v1.4f Standard LaTeX file (size option)
 caption.sty    2008/08/02 v3.1i Customizing captions (AR)
caption3.sty    2008/08/02 v3.1i caption3 kernel (AR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
 ***********
As you can see your version of caption is not updated and that's why you are getting those errors.

My example code compiles OK in any system that uses a recent version of caption.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

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

Post by phi »

OK, you're right, but I have the most recent system that is available in my distribution (TeXLive 2007). I won't update it simply because of this change. On the contrary, documents that compile on my system compile nearly nowhere else because nearly no one has a system from 2007 or later (really!). So my system is rather too new, not too old. ;-)
thrillRseekR
Posts: 2
Joined: Mon Nov 17, 2008 3:34 am

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

Post by thrillRseekR »

Thanks for your help.

I added the caption package passing justification=centering to it as mentioned and it works fine. It is definitely worthy to reiterate that the optional short captions are a necessity and that \newline does not always perform the same function as \\ does (or at least I don't think it did for me).
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\newline (or equivalent) inside a \caption

Post by gmedina »

thrillRseekR wrote:Thanks for your help.

I added the caption package passing justification=centering to it as mentioned and it works fine. It is definitely worthy to reiterate that the optional short captions are a necessity and that \newline does not always perform the same function as \\ does (or at least I don't think it did for me).
No. The optional argument is not necessary. What is really necessary is to use an updated version of the caption package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply