3

In this example, I want to insert the percentage of the page that isn't blank including the strikeouts and the percentage itself.

I tried doing both a Google search and a TeX.SE search but nothing came up.

enter image description here

10
  • i really doubt that your question is latex related ...
    – Zarko
    Nov 11 '17 at 3:31
  • @Zarko Why not? You can make graphics, animation, with additional packages. This is also still typesetting-related. I think this is like the ones shown in the TeX showcase.
    – teed
    Nov 11 '17 at 3:36
  • if you say so, ... but your question seems to be related to percentage calculation and very elementary geometry. if not, than provide some latex code, by which you try to solve your problem.
    – Zarko
    Nov 11 '17 at 3:41
  • 2
    Apart from the fact that this endeavour is silly, use 42, which is correct 100% of times.
    – Johannes_B
    Nov 11 '17 at 7:34
  • 1
    Genuine question... why do you actually want to do this? Nov 11 '17 at 11:29
8

Here is a possible solution through scripting. First, the TeX document (mydoc.tex):

\documentclass{article}

\begin{document}
\thispagestyle{empty}
This page is not entirely blank.
\end{document}

I could embed the conversion inside my script, but let us simplify the process:

$ convert mydoc.pdf -density 300 -flatten img.png

Now, consider the following Python code (howmuch.py):

from PIL import Image
ps = Image.open('img.png').getdata()
bp = 0
for p in ps:
    if p != 255:
        bp = bp + 1
print('Your page is {:.4f}% blank.'.format((1 - bp / float(len(ps))) * 100))

Then it is just a matter of issuing:

$ python howmuch.py 
Your page is 99.9104% blank.

Done.

Update: Thanks to Torbjørn, here is an improved version which attempts to guess the closest value based on previous iterations. I believe the logic behind the code is pretty self-explanatory.

from PIL import Image
from subprocess import call

def calculate(template, context, engine = 'pdflatex'):
    with open('page.tex', 'w') as page:
        page.write(template.format(**context))
    call([engine, 'page.tex'])
    call(['convert', 'page.pdf', '-density', '300', '-flatten', 'img.png'])
    ps = Image.open('img.png').getdata()
    bp = 0
    for p in ps:
            bp += 1 if p != 255 else 0
    return (1 - bp / float(len(ps))) * 100

doc = r'''
\documentclass{{article}}
\begin{{document}}
\thispagestyle{{empty}}
This page is not entirely blank.

It is {percentage}\% empty.
\end{{document}}
'''

guess = 99.8
max_attempts = 99
attempt_count = 0
tolerance = 0.01

while attempt_count < max_attempts:
    d = calculate(doc, { 'percentage' : guess }) - guess
    if abs(d) < tolerance:
        break
    else:
        guess = guess - 0.01 if d < 0 else guess + 0.01
    attempt_count += 1

print('Done.')

In this particular case, after 5 iterations, we achieve convergence. :)

Quack

And there we go.

8
  • I think the OP want's the percentage typeset to the PDF as well....
    – user4686
    Nov 11 '17 at 11:24
  • @jfbu: yes, you are right. Torbjørn and I were talking about this in the chat room. :) The script just calculates the blank area, so it might be good to include a percentage placeholder in the TeX document and fiddle with the result. We believe that, at some point, it will converge. :) Nov 11 '17 at 11:29
  • I trust it will :) anyway if you keep trace of previous one then python side will be easy place to add some stopping criteria say if does not move by more than 0.01% ...
    – user4686
    Nov 11 '17 at 11:31
  • hmm my last comment is a bit confused. Looking forward to see your final solution ;-)
    – user4686
    Nov 11 '17 at 11:32
  • @jfbu: oh no, the pressure is on! :) I will take a look later on, otherwise I will be procrastinating too much (and my thesis won't be written at all). Another possible workaround is to ignore the decimal places. :P Nov 11 '17 at 11:38
2

Although I have found a way to display it, the method I used wasn't using either calculations or LaTeX.

So, my question is still unsolved.

1) Compile with PdfLaTeX or XeTeX

2) Open PDF with Photoshop

3) Rasterize with at least 3000 dpi, grayscale, no anti-aliasing

4) Open Histogram and look at the percentile value of the one in the far right (the one in the far left is the amount of black)

5) If the percentile value is equal to the percent you typed, then well done. If not, then change the percent you typed and repeat steps 1 to 5 until you can arrive a correct result.

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.