1

I am creating an image grid with many images by using the PGF groupplots library. I am using a for-loop with \pgfplotsforeachungrouped.

Each filename is structured as follows: preamble_title_percent.jpg. I want to extract the title and the percent value by splitting the filename. Consequently, I can use the title and percent as values in \nextgroupplot. How can I do it?

Unlike an ordinary \foreach in PGF, \pgfplotsforeachungrouped only works with two arguments. That's why, I guess, it is the only way to extract the title and percent on the fly within \edef.

Pseudo-code:

\pgfplotsforeachungrouped \filename in {
 preamble_title_percent.jpg
}
{
    \edef\temp{
            # How can I split the filename here?
            \def\title{???}
            \def\percent{???}
            \noexpand\nextgroupplot[
            title=\title,
            scale only axis,            
            enlargelimits=false,
            axis on top,
            xticklabels={},
            yticklabels={},
            colormap={blackwhite}{[1pt]
            rgb255(0 pt)=(0, 63, 92);
            rgb255(\percent pt)=(0, 63, 92);
            rgb255(16 pt)=(166,188,198);
            rgb255(20pt)=(166,188,198)
            },
            colorbar,
            point meta min = 0,
            point meta max = 20,
            colorbar style = {
            at={(0.9,0.65)},anchor=north west,
            height = 3cm,
            width = 0.3cm,
            yticklabel pos=left,
            yticklabel style = {
            text width = 2em,
            align = right,
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision = 0,
            /tikz/.cd
        }
    }
    ]
    \noexpand\addplot[thick]
    graphics [
        xmin=0,
        ymin=0, 
        xmax=600,
        ymax=450
    ]{\noexpand\root /\filename}; % 
    }
    \temp
}

1 Answer 1

0

With xparse, which is now built into most TeX distributions, you can use \SplitArgument to split an argument at a certain character, including special characters. If we define a \splitAtUnder macro for underscores and \splitAtPeriod for periods, we can extract both the title and percent value from the name of the file.

\documentclass{standalone}
%\usepackage{xparse} % uncomment if you have an older TeX distribution
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}

\NewDocumentCommand\splitAtUnder {>{\SplitArgument{2}{_}} m}{\assignValues#1}
\NewDocumentCommand\splitAtPeriod{>{\SplitArgument{1}{.}} m}{\assignPercent#1}

\def\assignValues#1#2#3{
  \gdef\subplotTitle{#2}
  \splitAtPeriod{#3}
}
\def\assignPercent#1#2{
  \gdef\percent{#1}
}

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[group style={{group size=2 by 2},vertical sep=2cm}]
\pgfplotsforeachungrouped \filename in {
  example_cat_10.jpg,
  example_dog_20.jpg,
  example_duck_30.jpg,
  example_penguin_40.jpg
}
{
  \edef\splitAtUnderAxis{
    \splitAtUnder{\filename}
  }
  \splitAtUnderAxis
  \edef\setAxis{
    \noexpand\nextgroupplot[
      title=\noexpand\bfseries\subplotTitle,
      colormap={blackwhite}{[1pt]
        rgb255(0pt)         = (  0, 63, 92);
        rgb255(\percent pt) = (  0, 63, 92);
        rgb255(50pt)        = (166,188,198);
        rgb255(60pt)        = (166,188,198);
      },
    ]
    \noexpand\addplot[thick] graphics[xmin=0,ymin=0,xmax=600,ymax=450]{\filename};
  }
  \setAxis
}
\end{groupplot}
\end{tikzpicture}

\end{document}

Let's see what this code actually produces. Given four images with the following file names,

example_cat_10.jpg
example_dog_20.jpg
example_duck_30.jpg
example_penguin_40.jpg

then PGFPlots will automatically title each plot with a name given by the middle part of the image file name:

A group of four plots, arranged two by two. In reading order, the plots are of a cat, dog, duck, and penguin. Each plot has a title that matches the animal depicted.

(All four images are from Pixabay.)

1
  • worked like a charm! Thank u.
    – luwi
    Commented Aug 4, 2023 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .