If your system is a Unix one, the following set of macros will do, but they require that the file is compiled with pdflatex -shell-escape
:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[catalan]{babel}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\figureloop}{m}
{
\group_begin:
\etex_everyeof:D { \exp_not:N }
\tex_endlinechar:D \c_minus_one
\seq_gset_split:Nnx \g_figloop_names_seq { , }
{ \tex_input:D |"ls ~ -m ~ #1*.png" ~}
\group_end:
\seq_map_inline:Nn \g_figloop_names_seq { \dofigureloop { ##1 } }
}
\cs_generate_variant:Nn \seq_gset_split:Nnn {Nnx}
\seq_new:N \g_figloop_names_seq
\ExplSyntaxOff
\NewDocumentCommand \dofigureloop {m}
{
\begin{figure}
\centering
\includegraphics[width=10cm,height=10cm]{#1}
\caption[Sèries temporals de vent i direccions]
{Sèries temporals velocitat de vent i direccions. Filtratge de pics i glaçades de velocitat.}
\label{Serie-#1}
\end{figure}
}
\begin{document}
\figureloop{figure}
\end{document}
The argument to \figureloop
is the common part in the names of your figures. It might be generalized to different extensions.
The \tex_input:D |"ls~-m~#1*.png"~
bit loads the output of the ls
command sent via shell-escape to the operating system. This output is stored in a sequence that is then split. Then a mapping function is called on the sequence, its argument is exactly a file name and the macro called just typesets the necessary figure environment. A label in the form Serie-<filename>
is also generated.
With a different shell command this may be adapted also to Windows systems.
A simpler version, that requires -shell-escape
anyway; using kpsewhich
should ensure it can be executed as is also on Windows systems.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[catalan]{babel}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\figureloop}{m}
{
\jonel_figureloop:n { #1 }
}
\ior_new:N \g_jonel_ls_stream
\seq_new:N \l_jonel_filenames_seq
\cs_new_protected:Npn \jonel_figureloop:n #1
{
\seq_clear:N \l_jonel_filenames_seq
\ior_open:Nn \g_jonel_ls_stream { "|kpsewhich ~ #1*.png" }
\ior_map_inline:Nn \g_jonel_ls_stream
{ \seq_put_right:Nf \l_jonel_filenames_seq { \tl_trim_spaces:n { ##1 } } }
\seq_map_inline:Nn \l_jonel_filenames_seq { \dofigureloop { ##1 } }
}
\cs_generate_variant:Nn \seq_put_right:Nn {Nf}
\ExplSyntaxOff
\NewDocumentCommand \dofigureloop {m}
{
\begin{figure}
\centering
\includegraphics[width=10cm,height=10cm]{#1}
\caption[Sèries temporals de vent i direccions]
{Sèries temporals velocitat de vent i direccions.
Filtratge de pics i glaçades de velocitat.}
\label{Serie-#1}
\end{figure}
}
\begin{document}
% assume all figures are in the "figures" subfolder
% and are named "figureXXX.png"
\figureloop{figures/figure}
\end{document}
Update 2019
Due to changes in expl3
, the previous solution will no longer work. Here's a new version.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[catalan]{babel}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{showlabels}
\ExplSyntaxOn
\NewDocumentCommand{\figureloop}{O{png}mm}
{
\jonel_figureloop:nnn { #1 } { #2 } { #3 }
}
\tl_new:N \l_jonel_filenames_tl
\seq_new:N \l_jonel_filenames_seq
\cs_new_protected:Nn \__jonel_filenames_process:nn {}
\cs_generate_variant:Nn \__jonel_filenames_process:nn { VV }
\cs_new_protected:Nn \jonel_figureloop:nnn
{
\sys_shell_get:nnN { kpsewhich ~ #2*.#1 } {} \l_jonel_filenames_tl
\seq_set_split:NnV \l_jonel_filenames_seq { ~ } \l_jonel_filenames_tl
\seq_pop_right:NN \l_jonel_filenames_seq \l_tmpa_tl
\cs_set_protected:Nn \__jonel_filenames_process:nn { #3 }
\seq_map_variable:NNn \l_jonel_filenames_seq \l__jonel_filenames_fullname_tl
{
\tl_set_eq:NN \l__jonel_filenames_shortname_tl \l__jonel_filenames_fullname_tl
\tl_remove_once:Nn \l__jonel_filenames_shortname_tl { ./#2 }
\tl_remove_once:Nn \l__jonel_filenames_shortname_tl { .#1 }
\__jonel_filenames_process:VV \l__jonel_filenames_fullname_tl \l__jonel_filenames_shortname_tl
}
}
\ExplSyntaxOff
\begin{document}
% assume all figures are in the "figures" subfolder
% and are named "figureXXX.png"
\figureloop{figures/ScreenShot}{
\begin{figure}
\centering
\showtokens{\#1=#1,\#2=#2}
\includegraphics[width=10cm,height=10cm,keepaspectratio]{#1}
\caption[Sèries temporals de vent i direccions]
{Sèries temporals velocitat de vent i direccions.
Filtratge de pics i glaçades de velocitat.}
\label{Serie-#2}
\end{figure}
}
\end{document}
