Graphics, Figures & Tablesbchart | Issue with Caption for a Chart

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
vivs
Posts: 2
Joined: Sun Jan 27, 2013 5:50 am

bchart | Issue with Caption for a Chart

Post by vivs »

I keep getting an error "\caption outside float" and have been fumbling around with it for quite a while. I tried the solution from here but it still didn't solve my problem. Here is a snippet of my code:

Code: Select all

\documentclass[12pt,a4paper]{article}
\usepackage{bchart}

\begin{document}
  \begin{bchart}[step=50,max=150]
    \bcbar[value=OLS]{134.07}
    \bcbar[value=LASSO]{104.74}
    \bclabel{IQ}
    \bcbar[value=LASSO-OLS]{97.83}
    \bcbar[value=Relaxed LASSO]{94.48}
    \caption{\textbf{Project I Comparison Plot of Cross-Validated Prediction Errors.} Graphical comparison of OLS, LASSO, OLS-post-LASSO, and relaxed LASSO cross-validated prediction errors for Project I. Note: CGIS results are not presented (all prediction errors \textless0.5).}
    \label{tab:p1lassoext}
  \end{bchart}
\end{document}
Is there something blatantly obvious and wrong in my code? Or does it have to do with the package bchart?

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

bchart | Issue with Caption for a Chart

Post by localghost »

The bchart environment from the bchart package is not a floating object like {figure} or {table}. You could either put it into a usual {figure} environment (since it is a figure) or create a new floating object called e.g. chart by the newfloat package (from the caption bundle). The latter approach is shown in the below code.

Code: Select all

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}

\usepackage{newfloat}
\DeclareFloatingEnvironment[
  fileext=loc
]{chart}

\usepackage{caption}
\captionsetup{
  tableposition=top
}
\usepackage{bchart}

\begin{document}
  \listofcharts

  \begin{chart}[!ht]
    \centering
    \begin{bchart}[step=50,max=150]
      \bcbar[value=OLS]{134.07}
      \bcbar[value=LASSO]{104.74}
      \bclabel{IQ}
      \bcbar[value=LASSO-OLS]{97.83}
      \bcbar[value=Relaxed LASSO]{94.48}
    \end{bchart}
    \caption[Project I Comparison Plot of Cross-Validated Prediction Errors]{\textbf{Project I Comparison Plot of Cross-Validated Prediction Errors.} Graphical comparison of OLS, LASSO, OLS-post-LASSO, and relaxed LASSO cross-validated prediction errors for Project I. Note: CGIS results are not presented (all prediction errors \textless 0.5).}
    \label{tab:p1lassoext}
  \end{chart}
\end{document}
This approach has several advantages.
  • Those charts get independent numbering from all other floating objects.
  • They also get an own list like the LoF or LoT (see code above).
  • The appearance of their captions could be customized individually by the caption package.
Further reading:

Best regards and welcome to the board
Thorsten
vivs
Posts: 2
Joined: Sun Jan 27, 2013 5:50 am

Re: bchart | Issue with Caption for a Chart

Post by vivs »

Thanks for the quick response! It worked very nicely. Unfortunately I need to have it be in sequential order from my previous figures. Is there a way around that with the newfloat package?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

bchart | Issue with Caption for a Chart

Post by localghost »

vivs wrote:[…] Unfortunately I need to have it be in sequential order from my previous figures. Is there a way around that with the newfloat package?
Then simply think one step further and replace chart with the default {figure} environment in my example. I already mentioned that in my last reply. You can drop all the code related to newfloat in this case.
Post Reply