Graphics, Figures & TablesSending a caption to an environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
generator
Posts: 9
Joined: Sat Apr 03, 2010 5:56 am

Sending a caption to an environment

Post by generator »

I have the following minimal code:

Code: Select all

\documentclass[twocolumn]{article}
 
\usepackage{float}

\floatstyle{boxed}
\newfloat{ColumnFigure}{h}{cf1}
\floatname{ColumnFigure}{Figure}

\newenvironment{ColumnEnv}
{
\begin{ColumnFigure}
}
{
\caption{\textbf{Argument 1}}
\end{ColumnFigure}
}

\begin{document}
\begin{ColumnEnv}
Text inside the ColumnFigure.
\end{ColumnEnv}
\end{document}
I'd like to extend the environment so that it will accept captions. In other words, I'd like to use a standard command like "\begin{ColumnEnv[caption]}" or something similar that will accept captions as an input. Could someone show me code that accomplishes this?

Recommended reading 2024:

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

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

sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Sending a caption to an environment

Post by sommerfee »

You can save the argument of the environment into a command and use it in the 2nd part of the \newenvironment definition, e.g.:

Code: Select all

\documentclass[twocolumn]{article}

\usepackage{float}

\floatstyle{boxed}
\newfloat{ColumnFigure}{h}{cf1}
\floatname{ColumnFigure}{Figure}

\newenvironment{ColumnEnv}[1]
{%
\begin{ColumnFigure}%
\newcommand\ColumnFigureCaption{#1}%
}
{%
\caption{\textbf{\ColumnFigureCaption}}%
\end{ColumnFigure}%
}

\begin{document}
\begin{ColumnEnv}{Argument 1}
Text inside the ColumnFigure.
\end{ColumnEnv}
\end{document}
But since the float package don't care where the \caption command will be placed, you can simply place the \caption command inside the 1st half of the \newenviroment definition:

Code: Select all

\documentclass[twocolumn]{article}

\usepackage{float}

\floatstyle{boxed}
\newfloat{ColumnFigure}{h}{cf1}
\floatname{ColumnFigure}{Figure}

\newenvironment{ColumnEnv}[1]
{%
\begin{ColumnFigure}%
\caption{\textbf{#1}}%
}
{%
\end{ColumnFigure}%
}

\begin{document}
\begin{ColumnEnv}{Argument 1}
Text inside the ColumnFigure.
\end{ColumnEnv}
\end{document}
Post Reply