Graphics, Figures & TablesAlter Table Caption Prefix

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
luis
Posts: 14
Joined: Wed Dec 30, 2009 9:03 pm

Alter Table Caption Prefix

Post by luis »

Hello everyone,

I'd like to change the caption prefix of a table, e.g. like this.

Code: Select all

\begin{table}
\caption{A caption}
\end{table}
This results in something like this.
Table I: A caption
And I'd like to change it to this.
Listing I: A caption
I tried using the \tablename command, but it has no effect:

Code: Select all

\begin{table}
\def\tablename{Listing I}
\caption{A caption}
\end{table}
I also have tried it with \renewcommand*, but it doesn't have any effect.

Code: Select all

\renewcommand*{\tablename}{Listing}
\begin{table}
\caption{A caption}
\end{table}
How could this be made? Thank you,

Luís

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
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Alter Table Caption Prefix

Post by localghost »

There are some smarter alternatives (in order of priority).
  • Use a package like listings or minted to typeset listings. These packages provide a float environment with the corresponding prefix.
  • Use a package like floatrow or newfloat (from the caption bundle) to create a new float environment with the corresponding prefix.

Thorsten
luis
Posts: 14
Joined: Wed Dec 30, 2009 9:03 pm

Alter Table Caption Prefix

Post by luis »

Hi Thorsten,

Neither listings (which I already tried) nor minted seem to apply in this case. I tried newfloat, but unfortunately the document doesn't compile if add the package command. As for floatrow its manual has over 100 pages and so far I haven't found an example where the float element prefix is changed.

Thanks for answering in any case.

Luís
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Alter Table Caption Prefix

Post by sommerfee »

luis wrote:I tried newfloat, but unfortunatelly the document doesn't compile if add the package command.
What do you mean by "doesn't compile"? Do you get an error message? If yes, which one? (What document class do you use?)
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Alter Table Caption Prefix

Post by localghost »

luis wrote:[…] Neither listings (which I already tried) nor minted seem to apply in this case. […]
Why? Give a minimal example where these packages fail.
luis wrote:[…] I tried newfloat, but unfortunately the document doesn't compile if add the package command. […]
Same questions as sommerfee already asked.
luis wrote:[…] As for floatrow its manual has over 100 pages and so far I haven't found an example where the float element prefix is changed. […]
So you should read Section 4 of its manual (which can be found in the ToC).
luis
Posts: 14
Joined: Wed Dec 30, 2009 9:03 pm

Alter Table Caption Prefix

Post by luis »

Hi localghot and sommerfee, thank you for replying. My comments go below.
localghost wrote:Why? Give a minimal example where these packages fail.
I don't know where they fail, but I couldn't find examples where these packages are used to substitute a table caption prefix.
sommerfee wrote:What do you mean by "doesn't compile"? Do you get an error message? If yes, which one? (What document class do you use?)
I get this error:
LaTeXError: File `newfloat.sty' not found.
localghost wrote:So you should read Section 4 of its manual (which can be found in the ToC).
Section 4 of the manual I have focus on new float types, not on captions. With this particular package I tried this set up:

Code: Select all

\captionsetup[table]{name=Listing}
But it has not effect on the caption prefix.

Any other hints are welcome. Thank you,

Luís
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Alter Table Caption Prefix

Post by sommerfee »

luis wrote:I get this error:
LaTeXError: File `newfloat.sty' not found.
Then you simply do not have the newfloat package installed. Updating your TeX system (or at least the caption package bundle) should help.

As an alternative you could give the floatrow or the float package a try.
I tried this set up:

Code: Select all

\captionsetup[table]{name=Listing}
But it has not effect on the caption prefix.
Please give us a minimal example.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Alter Table Caption Prefix

Post by localghost »

luis wrote:[…] I don't know where they fail, but I couldn't find examples where these packages are used to substitute a table caption prefix. […]
Of course they don't alter table captions. They offer new environments that are labeled as listings. At the moment I can't comprehend why you insist on altering the table caption prefix. Obviously you need something that you can label as listing. So it would be much smarter to create a new floating object or use a package that allows you to typeset listings. The corresponding tools have been mentioned. Perhaps you can elaborate what you want to put into the {table} environment and then label as a listing. Then it would be much easier to suggest a possible solution.
luis wrote:[…] Section 4 of the manual I have focus on new float types, not on captions. […]
The package allows to create a new float that you can label according to your needs. And Section 4 describes how to get one that is labeled e.g. as listing.
luis
Posts: 14
Joined: Wed Dec 30, 2009 9:03 pm

Alter Table Caption Prefix

Post by luis »

Hello everyone, thank you for the replies.

First on this comment:
localghost wrote:At the moment I can't comprehend why you insist on altering the table caption prefix. Obviously you need something that you can label as listing. So it would be much smarter to create a new floating object or use a package that allows you to typeset listings.
And I don't understand why you insist on me doing something different from what I initially presented. Again, the use case is very simple: I have a {tabular} environment inside a {table} and I need to change its caption prefix from "Table" to "Listing". I do not want to change the contents of the floating environment, I just want to change the caption.

Regarding the newfloat package I have no idea why is it missing, I regularly run the updates on this system.

On defining a new floating environment, while I am able to change the prefix with floatrow, the caption is wrongly placed and seems to miss some of the presets of the {table} environment. Again, I'd say this way is not what I need, here's a minimum example:

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}

\usepackage{floatrow}

\DeclareNewFloatType{mylist}%
	{name=Listing}

\begin{document}

\begin{mylist}
\caption{The caption}
   \begin{center}
	\begin{tabular}{| r | r |}
	\hline
	This is & a table. \\
	\hline
    \end{tabular}
   \end{center}
\end{mylist}

\end{document}
Reading the floatrow manual the only example that seems to exactly fit the use case I'm presenting is using \captionsetup. Unfortunately this produces no effect, here's the example:

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}

\usepackage{floatrow}

\begin{document}

\captionsetup[table]{name=Listing}

\begin{table}
\caption{The caption}
   \begin{center}
	\begin{tabular}{| r | r |}
	\hline
	This is & a table. \\
	\hline
    \end{tabular}
   \end{center}
\end{table}

\end{document}
If someone can tell me why \captionsetup isn't working that would be great. Otherwise, using a new floating environment, if it is possible, things must be a bit more refined than what is shown in the manual.

Thanks once more,
Luís
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Alter Table Caption Prefix

Post by sommerfee »

luis wrote:And I don't understand why you insist on me doing something different from what I initially presented.
Because what localghost suggests is the "natural" way. The floating environments {figure}, {table}, ..., have distinct names, counters, and lists, so if someone needs "listing"s, the choice of creating a new (floating) environment is self-evident while the choice of abusing an existing one is not.
Again, the use case is very simple: I have a {tabular} environment inside a {table} and I need to change its caption prefix from "Table" to "Listing". I do not want to change the contents of the floating environment, I just want to change the caption.

Code: Select all

\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{table}
\captionsetup{name=Listing}
\centering
\begin{tabular}{cc}
A & B\\
C & D\\
\end{tabular}
\caption{A listing}
\end{table}
\end{document}
This approach may have some unwanted side-effects, but this is what you want, so this is what you get.
Regarding the newfloat package I have no idea why is it missing, I regularly run the updates on this system.
The newfloat package was introduced November 2011, so no, your TeX system is not up-to-date.
On defining a new floating environment, while I am able to change the prefix with floatrow, the caption is wrongly placed
What caption placement do you want? Using the floatrow package you can adjust the placement by options of \DeclareNewFloatType.
and seems to miss some of the presets of the {table} environment.
What presets are you missing?
Post Reply