Following this question I asked a few years ago, I'm looking for a way to find the width of the largest picture (in width) in my document.

Typically, this requires a for loop and some TeX programming. Since my compilation process already includes 3 parsing steps (required to compute the last page number and bibliography items with biber), this isn't really a problem for me if this requires yet another one. If I can rely on an existing compilation pass, that would be even greater though.

Any idea how I can write this parser? My solution would have to use some regexes and manual parsing using Python, but I would like to avoid to require yet another engine.

Why this parser? Let me explain what I want by an example.


  • a full screen screenshot displayed with \includegraphics with a with specified by \textwidth.

    enter image description here

  • another screenshot taken on the same computer, but cropped to a specific size (e.g. to only have a specific dialog or a subset of the first image to focus on something specific).

    enter image description here

The idea is to get the size of the first picture to be able to determine the ratio I need to specify to the second picture (how to compute it in the \includegraphics statement? do we have support for some mathematics computation in this context?).

In this use case, the first picture is the one one, but I would like to be able to specify the pictures kept to determine which one is the largest (in width, with a special tag for example).

As you can see, the way the pictures are presented here above in this question are incorrect. I want the second picture representing only the dialog from the first image to be the same size as it is in the first image. Typically, this is what I want (screenshot taken on LibreOffice Writer):

enter image description here

As you can see the dialog in the second image has the same width size as the first image (I don't know if we can call dpi in this context).

  • 1
    I'm not clear what problem exactly you want to solve. If you want to show all pictures at the same width, what prevents you from just specifying an arbitrary width -- say, 0.75\textwidth? Or do you want to show the all at the same resolution (dpi)? Sep 3, 2017 at 22:41
  • 2
    Why don't you just create a dimension of zero, then set it to the width of the current image if the latter exceeds its current value, each time an image is encountered? Write it to file at the end of the document and use it on the next run. Either use a switch to toggle or include each twice each run: once to measure and once to set. You just need to make sure the dimension is set after the dimension is initialised. However, it really isn't obvious why you can't do as @MichaelPalmer suggested and, anyway, there's no code. I don't understand why you want to parse it separately, especially.
    – cfr
    Sep 4, 2017 at 0:42
  • 1
    If all you are trying to do is measure the size of an image, put it into a savebox. For \sbox0 you would get \wd0 for the width, \ht0 for the height (baseline to top) and \dp0 for the depth (baseline to bottom). \includegraphics puts the baseline at the bottom. Sep 4, 2017 at 13:27
  • 1
    In that case, the most straightforward approach would be to set a defined DPI into your image files, e.g. using convert from the imagemagick library: for pic in *.png; do convert -units PixelsPerInch $pic -density 300 $pic; done. Either experiment with the DPI number, or define a custom scaling factor inside LaTeX \newcommand{\myscale}{0.75}, which you then use with every screenshot: \includegraphics[scale=\myscale]{screenshot1}. Sep 4, 2017 at 14:06
  • 1
    @MichaelPalmer Indeed, (if I'm not wrong) pdfimageresolution seems to be provided by pdftex and not by my xelatex engine: ! Undefined control sequence. <recently read> \pdfimageresolution. Actually digging in my own class implementation is what I do from the beginning, but sbox/mboxes are still not very clear for me in this context. Parsing a *TeX file isn't something I already performed either.
    – wget
    Sep 4, 2017 at 14:45


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.