0

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.

Considering

  • 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).

wget
  • 782
  • 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)? – Michael Palmer Sep 03 '17 at 22:41
  • DPI doesn't really have any meaning here @MichaelPalmer. There was a question about this recently. – cfr Sep 04 '17 at 00:37
  • What have you tried? What is the problem? What's a minimal compilable example demonstrating the issue? You've tagged this XeTeX, so I assume you're using this engine. graphicx keys probably means the LaTeX format. Likely you're using 2e. – cfr Sep 04 '17 at 00:39
  • 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 04 '17 at 00:42
  • @MichaelPalmer and cfr I updated my question in order to be clearer. – wget Sep 04 '17 at 11:22
  • OK. If you want to achieve that, you should make sure to take all your screenshots at the same zoom level, because otherwise LaTeX really has no way of knowing the correct size. Next, you simply need to make sure that all images are shown at the same resolution (DPI=dots per inch). You can do that by setting e.g. \pdfimageresolution=300 in the preamble. Higher numbers mean more pixels per inch, that is, smaller image size. No need for any other computations. – Michael Palmer Sep 04 '17 at 13:09
  • Note that bitmap graphics (such as PNG and JPG) may contain DPI information; whether or not it does will depend on the graphics software you use. It seems that if such info is there, then it will override the \pdfimageresolution setting. You can delete the stored DPI information from your picture using exiftool, like so: exiftool -all= mygreatpic.png (note the space after =). After that, your \pdfimageresolution setting should take effect. – Michael Palmer Sep 04 '17 at 13:23
  • 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. – John Kormylo Sep 04 '17 at 13:27
  • @JohnKormylo yes, this is the solution DavidCarlisle recommended me to use. He said via the chat he'll be able to provide me an implementation maybe already this evening. I'll thus wait :) – wget Sep 04 '17 at 13:45
  • @MichaelPalmer Using \pdfimageresolution might break the other images I have included, and I don't know whether this command is available in the engine I use (xelatex). – wget Sep 04 '17 at 13:47
  • 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}. – Michael Palmer Sep 04 '17 at 14:06
  • I like advanced \savebox trickery as well as anyone, but there really is no need for it here; the impression only arose because you didn't think through your problem. All you need is a consistent DPI setting, which you manually tweak until your widest picture fits into \textwidth. – Michael Palmer Sep 04 '17 at 14:11
  • Also, if you don't know if xelatex offers \pdfimageresolution, you might go to the extreme of testing it. How about putting in a little work yourself to solve your own problems? – Michael Palmer Sep 04 '17 at 14:13
  • 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 04 '17 at 14:45
  • You don't need any sboxes here. Take your widest picture, tweak DPI and/or custom \myscale parameter for \includegraphics until it looks right, and apply the same settings to all other pictures. If you need to guard against outsize pictures, \usepackage[export]{adjustbox}, which then allows you to say \includegraphics[scale=\myscale,max height=\textheight, max width=\textwidth]{pic}. – Michael Palmer Sep 04 '17 at 15:01
  • 1
    Measuring an image, storing the size and using them later (even with calculations) is easy. But I think you haven't really thought through how and where you want to use the values. E.g. I do find it quite improbable that you will have only one "master" image. – Ulrike Fischer Sep 04 '17 at 20:30

0 Answers0