Graphics, Figures & TablesGenerate figure label with macro

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
stridder
Posts: 6
Joined: Sun Mar 13, 2016 9:43 am

Generate figure label with macro

Post by stridder »

Hello,
I am trying to generate figure labels with macro like this:

Code: Select all

\newcommand{\getLabel}[3]{
	\newcommand{\Stat}{\StrLeft{#1}{3}}
	fig:{\Stat}Re#2DOF#3 %1st 3 letters of #2
}
where \StrLeft comes from xstring package. I use above macro inside figure environment like this:

Code: Select all

\label{\getLabel{recirculation point}{50}{4M}}
and want to use the same macro for referencing as: \ref{\getLabel{recirculation point}{50}{4M}}.
The 1st parameter has space (I suspect space is not allowed to be part of label?), therefore I use \StrLeft to get first 3 letters for the label. Using only 2nd and 3rd parameters is not enough to generate unique label. It doesn’t really matter how the generated label will look like as long as it is unique (I expect to have a 100 page document with at least 50 figures).
The goal is to have a macro that plots figures and generates caption and label based on few parameters. Here is its code:

Code: Select all

\newcommand{\myplot}[4]{
	\begin{figure}[h!]
		\label{\getLabel{#2}{#3}{#4}}
		 \centering
		 \includegraphics[width=\linewidth]{#1}	 
		 \caption{#2 for #4 DOF mesh, Re #3.}
	\end{figure} 
}
But whenever I use \StrLeft inside \getLabel macro, there are lots of errors:
Argument of \caption@@@withoptargs has an extra }.
Extra }, or forgotten \endgroup. ...@d = *\def \par }{ on input line 336}}{9}} (followed by: )
Extra }, or forgotten \endgroup. \newlabel{ \let \reserved@d = *\def \par } (followed by: {{\caption@xref { \let \reser...)
Extra }, or forgotten \endgroup. \newlabel{ \let \reserved@d = *\def \par } (followed by: {{\caption@xref { \let \reser...)

and many more. When I remove \StrLeft, all errors disappear.
Is there a way to implement this?
Can figure label contain digits and/or spaces?
Thank you.

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

Re: Generate figure label with macro

Post by Johannes_B »

Little word of warning: around page 66 of your document you will find the third exception of your macro, around page 82 you will be crying asking yourself why you generated that macro in the first place.

A LaTeX macro is a bad idea in this context, an editor macro is a better one. Trust me ;-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
stridder
Posts: 6
Joined: Sun Mar 13, 2016 9:43 am

Generate figure label with macro

Post by stridder »

Johannes_B wrote:Little word of warning: around page 66 of your document you will find the third exception of your macro, around page 82 you will be crying asking yourself why you generated that macro in the first place.
Can you give a particular example of what you mean?
Are you saying that macro will not support all the cases I may need?
In this case I could just paste all those figure structures directly in text.
I am not trying to save bytes of text, but rather reduce the work on rewriting all 100 captions if my supervisor tells me e.g. to replace or change orders of words and improve readability of latex source (its easier to read just one line than 6, most of which don't really have any special meaning). And this may happen when I will be close to 100 pages.
Also I group plots in subfolders per chapter etc. If I change the name of some folder, I will have to fix 50 paths without a macro.
I am just starting with LaTeX, so believe there may be better ways. Actually this is the main reason, since I don't know how to do it all correctly for now.
Johannes_B wrote: A LaTeX macro is a bad idea in this context, an editor macro is a better one. Trust me ;-)
Whats 'editor macro'?
Could you post your code how you implemented label generating?
Thanks
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Generate figure label with macro

Post by Johannes_B »

The most simple solution weems to be adding an extra argument and avoiding the trouble with xstring.

Code: Select all

\documentclass{article}
\usepackage{xstring}
\usepackage{graphicx}
\newcommand{\doLabel}[3]{%
	fig:{#1}Re#2DOF#3%
}
\newcommand{\myplot}[5]{
	\begin{figure}
		\centering
		\includegraphics[width=\linewidth]{#1}  
		\caption{#2 for #4 DOF mesh, Re #3.}
		\label{\doLabel{#5}{#3}{#4}}
	\end{figure}
}
\begin{document}
\myplot{example-image}{Wombat and other stuff}{Mara}{Capybara}{Wombat}
\ref{\doLabel{Wombat}{Mara}{Capybara}}
\end{document}
Setting up your editor to writing up all that stuff leaving blanks for you to fill in might be a better idea. Having so many arguments leads to confusion easily. It isn't really easier to read as well for an outstander. Having the caption consistent is a good point though, i would define a command just for that (and maybe dealing with the label as well).

Ensure that the label is placed after the caption.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply