Generalhow to use \autoref with a custom made float??

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

how to use \autoref with a custom made float??

Post by bjorn victor »

Hi all,

I have been playing with the FLOAT package and I have made a float called "Example". The caption is good, the list of examples is good, but the thing the bugs me is that I cannot use \autoref with my new float. I would like it to say Example 1.1 and not just 1.1. I am aware that my custom float is not in the standard repertoire of LaTeX or HYPERREF, so I will have to somehow tell HYPERREF about my new float. Question is how? I found some \def commands in the HYPERREF manual, but they just seem to modify the language depending on the babel option you specify. I found no way to make a "new" entry.
Any help is much appreciated!!

Bjorn

Code: Select all

\documentclass[a4paper,12pt]{report} 
\usepackage[a4paper,margin=2.5cm]{geometry}            
\usepackage{float} 
\usepackage[format=hang,labelfont={bf,sf},font=small]{caption,subfig}   
\setlength{\captionmargin}{20pt} 		
\usepackage[pdftex,bookmarks,colorlinks,linkcolor=black,urlcolor=black]{hyperref} 
\newfloat{example}{H}{flt}[chapter]
\floatname{example}{Example}

\begin{document} 
\listof{example}{List of examples}
\chapter{one}
\begin{example} 
\caption{tekst tekst tekst}
tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst
\label{ex1} 
\end{example}
This is a ref to \autoref{ex1}. (here I want to see ... a ref to Example 1.1)
\end{document}

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

how to use \autoref with a custom made float??

Post by gmedina »

Hi,

one quick solution would be to use the cleveref package (this might introduce some limitations, as explained in the documentation):

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[a4paper,margin=2.5cm]{geometry}           
\usepackage{float}
\usepackage[format=hang,labelfont={bf,sf},font=small]{caption,subfig}   
\setlength{\captionmargin}{20pt}       
\usepackage[pdftex,bookmarks,colorlinks,linkcolor=black,urlcolor=black]{hyperref}
\usepackage{cleveref}

\newfloat{example}{H}{flt}[chapter]
\floatname{example}{Example}

\crefname{example}{example}{examples}
\Crefname{example}{Example}{Examples}


\begin{document}

\listof{example}{List of examples}
\chapter{one}
\begin{example}
\caption{tekst tekst tekst}
tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst
\label{ex1}
\end{example}

This is a ref to \cref{ex1}.

\Cref{ex1} shows that...
\end{document}
I'll think about a solution not involving any additional packages.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

how to use \autoref with a custom made float??

Post by sommerfee »

Since you load my caption package anyway, you could simply use this package (instead of the float package) to define a new float type called "example". The caption package defines a name for \autoref automatically, so the following code should fulfill your needs:

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage[format=hang,labelfont={bf,sf},font=small]{caption,subfig}
\setlength{\captionmargin}{20pt}
\usepackage[bookmarks,colorlinks,linkcolor=black,urlcolor=black]{hyperref}

\DeclareCaptionType[fileext=flt]{example}

\begin{document}
\listofexamples
\chapter{one}
\begin{example}
\caption{tekst tekst tekst}
tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst
\label{ex1}
\end{example}
This is a ref to \autoref{ex1}. (here I want to see ... a ref to Example 1.1)
\end{document}
bjorn victor
Posts: 28
Joined: Fri Apr 13, 2007 11:17 am

how to use \autoref with a custom made float??

Post by bjorn victor »

@ sommerfee: indeed, the autoref command works, but for some reason, my float seems to be at the bottom of the page when I run your code.
Doesn't matter, I add the [h] command next to the begin{example} and it was fine.

Thank you very much!

I had no idea what your caption package could do!

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage[format=hang,labelfont={bf,sf},font=small]{caption,subfig}
\setlength{\captionmargin}{20pt}
\usepackage[bookmarks,colorlinks,linkcolor=black,urlcolor=black]{hyperref}

\DeclareCaptionType[fileext=flt]{example}

\begin{document}
\listofexamples
\chapter{one}
\begin{example}[h]
\caption{tekst tekst tekst}
tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst tekst
\label{ex1}
\end{example}
This is a ref to \autoref{ex1}.
\end{document}
Post Reply