Graphics, Figures & TablesShort caption problem -- figure vs table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
k123123
Posts: 6
Joined: Tue Aug 20, 2013 12:35 pm

Short caption problem -- figure vs table

Post by k123123 »

I noticed a strange problem using the \caption[shortcaption]{longcaption}, it seems to behave differently between the Figure and Table environments?

Note - I have created a command in the preamble so that I get a short caption only in the LOF, then for the actual figure the short caption is repeated as a 'title', and the long caption contains more details:

Code: Select all

\newcommand{\figCaption}[2]{\caption[#1]{\textbf{#1}. #2}}
becomes

Code: Select all

Figure 1: \textbf{Effect of adding drug X to live cells}. Drug dosage 10 mg for 1 hour, etc etc etc.
This command works perfectly for figures, but not for tables... and I can't seem to figure out why?

This is my thesis master document (I've cut out all the unnecessary packages); the example figures obviously won't be added but it seems the environment/float/caption is created ok without the actual figure.

It's MikTex 2.9 and I'm compiling with PdfTex


Code: Select all

\documentclass[a4paper, oneside, 11pt, openany]{book}

\usepackage{booktabs}
% for nicer looking tables; required if using the Excel2Latex macro

\usepackage[font=small,labelfont=bf]{caption}
% puts captions in small, with label in bold font
\captionsetup[table]{skip=4pt}
% change table caption to be a bit closer (default 10pt)


%Custom caption: short cap(bold) followed by long cap
\newcommand{\figCaption}[2]{\caption[#1]{\textbf{#1}. #2}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\mainmatter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\listoffigures

\section{Figure -- uses custom caption command}
\begin{figure}[htbp]
\begin{center}
%\includegraphics[scale=1]{chapter1/example_figure.pdf}
\figCaption{Figure:ShortCaption}{Figure:LongCaption}
\label{example_figure}
\end{center}
\end{figure}


\section{Figure -- uses built-in caption}
\begin{figure}[htbp]
\begin{center}
%\includegraphics[scale=1]{chapter1/example_figure.pdf}
\caption[Figure short cap]{Figure long cap}
\label{example_figure2}
\end{center}
\end{figure}


\listoftables

\section{Table -- uses custom command}
\begin{table}[htbp]
  \centering
  \figCaption[Table with short caption][Table long caption]
    \begin{tabular}{rr}
    \toprule
         Name & Time \\
    \midrule
    	Tom & 55 sec \\
        Tom & 55 sec \\
		Tom & 55 sec \\
        Tom & 55 sec \\
		Tom & 55 sec \\
    \bottomrule
    \end{tabular}%
	\label{table:example1}
\end{table}%

\section{Table -- uses built-in caption}
\begin{table}[htbp]
  \centering
  \caption[shortcaption]{longcaption}
    \begin{tabular}{rr}
    \toprule
         Name & Time \\
    \midrule
    	Tom & 55 sec \\
        Tom & 55 sec \\
		Tom & 55 sec \\
        Tom & 55 sec \\
		Tom & 55 sec \\
    \bottomrule
    \end{tabular}%
\end{table}%

\end{document}
(edit - put latex in [ code ])
Last edited by cgnieder on Wed Aug 21, 2013 10:57 pm, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Short caption problem -- figure vs table

Post by Johannes_B »

Hi, you just missed to give the right brackets ({ }). You gave [ ].

Code: Select all

\documentclass[oneside]{book}

\usepackage[font=small,labelfont=bf]{caption}
% puts captions in small, with label in bold font

%Custom caption: short cap(bold) followed by long cap
\newcommand{\figCaption}[2]{\caption[#1]{\textbf{#1}. #2}}

\begin{document}

\listoffigures

\begin{figure}
\figCaption{LoF and Title}{detailed LongCaption}
\label{example_figure}
\end{figure}

\begin{figure}
\caption[Figure short cap]{Figure long cap}
\label{example_figure2}
\end{figure}

\listoftables

\begin{table}
  \figCaption{Lot and Title}{Table  detailed long caption}
  \label{table:example1}
\end{table}%

\begin{table}
  \caption[shortcaption]{longcaption}
\end{table}%
\end{document}
I am not sure, but i guess you can do that using the caption-package.

Best regards
Johannes
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
k123123
Posts: 6
Joined: Tue Aug 20, 2013 12:35 pm

Re: Short caption problem -- figure vs table

Post by k123123 »

Wow, that is embarrassing. Thank you, it works fine now.
I set up the command in Texmaker to auto-fill... but I set up with the wrong brackets. The Figures were all ok {}{} because I was copy/pasting from a template that all my figures are based on. But for the occassional table, I was typing/autofilling from scratch so it had the wrong brackets. Oh well, I like a simple solution :D


I took a look at the 'caption' documentation, it says I can define a new style using:

\DeclareCaptionFormat{name}{code using #1, #2 and #3}
where #1 is the caption label, #2 is the separator and #3 is the text.

But I couldn't find anything relating to the arguments passed to the \caption[]{} command itself.

I actually followed this answer to get what I wanted:
http://www.latex-community.org/viewtopi ... =45&t=3522


Anyway thank you very much for spotting that!
Post Reply