# Most efficient workflow for displaying (imgur hosted) output of TeX & friends source?

Many seem to "effortlessly" (well, maybe it seems effortless because there's no effort on my part!) display the output of their TeX & friends source code in their posts. They must have some way to easily transfer output to a nice imgur link for inclusion?

If I were to try doing it, I imagine I'd have to take the following steps:

1. Generate source PDF.
2. Open in Adobe PDF reader, take snapshot of relevant bit -- on my slow laptop, this can take a while.
3. Paste into MS Paint, and save as png. On my slow laptop, this can take a while.
4. Tell SE where I have saved my picture when making the post.

Too much work!!! What's the best workflow to get this done, while having it be relatively effortless, without hiring a full time slave? I think based on the information in this question, I can shave off steps 2 and 3 by simply uploading the PDF? However, this is not working for me -- I get an error when uploading a PDF about it not being the right file type.

My (Linux) workflow is;

• Create the document with \documentclass{article}, and add \pagenumbering{gobble} to suppress page numbers.
• Run pdflatex file.tex.
• Run pdfcrop file.pdf.
• Run convert -density 300 file-crop.pdf file.png.

This is pretty fast (especially because it's automatable), and, IMO more importantly, produces very high quality output with transparent graphics. You're not rasterizing the PDF in a PDF reader and then screen-grabbing that with the background—instead, you're converting directly from pristine vector PDF to a transparent PNG. Doesn't get much better than that.

For vim/LaTeX-Suite users, condense this to three keystrokes (assuming you have the page numbering fixed already):

nnoremap <Leader>li :w<CR>:!pdflatex %; pdfcrop %<.pdf; convert -density 300 %<-crop.pdf %<.png<CR>


Throw an !xdg-open %<.png in there if you want to open the file.

• You have to create a file each time, and the compiler creates a .log and a .aux at the mimimum. How do you manage these? – Zoxume Feb 5 '15 at 16:16
• i usually find a couple more options to be useful: pdfcrop -margin 3 ... and convert -trim -density ...; this will create a minimum sized .png file without removing slivers from the outermost printed elements. – barbara beeton Feb 5 '15 at 17:34
• @T.D. -- regarding the .aux file, you can suppress its creation by inserting \nofiles after the \documentclass line. – barbara beeton Feb 5 '15 at 17:35
• @T.D.: You could also create a makefile to automate the process, which will process the file with auxiliaries as needed, but clean it up afterward. Otherwise, if you're managing references (which require an .aux file), using \nofiles would not help. – Werner Feb 6 '15 at 0:38
• Well actually it was not what I asked (altough I learned the existence of \nofiles — I already use makefiles). What I wanted to know is how you manage your .tex files. Do you have a series of them in a folder? Do you create and delete them on the fly? Do you have a unique file you erase everytime? – Zoxume Feb 6 '15 at 12:41
• @T.D. I mostly use a single test file. I erase the content when I want to create a new example or run a new bit of code to see a problem. Then I keep the PDF open in the viewer (so no need to open the PDF). If there's a solution I want to keep, I just copy the test file into another directory with a descriptive name. Then I use the image-copy-tool in my viewer to select the bit I want and tell it to save it to a PNG file. (Usually 1.png.) Confirm I want to overwrite the existing PNG and then upload it. Much quicker to do than to describe! – cfr Feb 8 '15 at 2:48

My workflow on Mac OS X:

1. Generate a PDF file with pdflatex, xelatex, lualatex or latex+dvips+ps2pdf as necessary
2. Open the PDF with Skim and choose suitable view settings
3. Press Command+Shift+4
4. Drag over the area I want to show; releasing the mouse/trackpad button produces a file named Screen Shot YYYY-MM-DD at HH.MM.SS.png on the Desktop
5. Upload the PNG to Imgur with the interface (the naming convention with date and hour ensures the most recent file is last among those produced this way, so it can be easily found)
• Same workflow here. – jub0bs Feb 6 '15 at 18:57
• It's what I used to do, but unfortunately I got that white background from Preview. – Sean Allred Feb 8 '15 at 23:47
• @SeanAllred Yes, the background is white. I much prefer it to transparent. – egreg Feb 9 '15 at 0:04
• @egreg Great idea! Skim at SourceForge That's a lot faster than CanvasSize adjustments in Photoshop. Maybe someone can just add this to egreg's answer as a comment. – Saphar Koshet Sep 29 '18 at 2:15

Just another option to give you an idea of some more possibilities. I use Windows 7, TeX Live, and TeXnicCenter. I have ImageMagick installed and on the system path.

I make a copy of each TeXnicCenter build profile I commonly use and rename it to pdflatex (TeX.SX) or similar. To each of the profiles, I add a post-processor step called "Create Image":

This will call ImageMagick's convert program at the end of each compilation, creating a .png of the output PDF with a resolution of 300 ppi. If the output has multiple pages, an image for each page is automatically created. convert can also produce animated .gifs, but I do these manually since I don't use them frequently.

I have one TeXnicCenter "project" for TeX.SX testing. This is set up to use my special build profiles (I just have to select a different workflow every once in a while, but most of the time it's pdflatex). This project is nearly always open on my computer, ready to go for rapid-fire testing/answering. :-)

So, I can copy an MWE, paste it in at the top of my file, and press F7 to compile. Once I have compiled a final result, I have a high-res PNG sitting in my working folder, ready to upload if necessary.

I use the standalone class wherever possible, to avoid the need for manual cropping. But if I need to crop manually, I just use the basic Windows Paint program because it's the fastest to open and works fine for basic cropping.

When I have new code to test, it gets pasted at the top of the file, so my last n answers/experiments are still in the .tex file, ready to be resurrected if necessary. Every month or so I clean house, removing all the accumulated code.

• Clever trick in adding new code to the top of the file. Somehow I forgot that everything after \end{document} (or other analogues to \bye) is just ignored :) – Sean Allred Feb 12 '15 at 3:38

From your reference to MS Paint, I assume you're running on Windows.

Here is my workflow, which is efficient for my use, and should be for yours as well:

1. Get familiar with a medium of taking a (partial) screen grab. There are a number of options out there, including

• PrtSc or Alt+PrtSc

The former grabs the entire window into the Clipboard, while the latter only grabs the active window. It works in a pinch, but no-one is interested in seeing the menu-items of your favourite editor/viewer when they're only interested in seeing the output.

2. As browser, use Google Chrome since it allows you to copy-and-paste a captured image when using the Ctrl+G interface.

The above approach works analogous to a regular copy-and-paste, but for images.

There are two scripts I use. One is called pdf2png – it takes a list of PDF files and places the PNGs (with the same base filenames) on my Desktop. I could use an imgur CLI application to automatically upload these, but that would not put them under StackExchange's account.

#!/usr/bin/env bash

for pdf in ${@}; do dir=$(dirname $pdf) base=$(basename -s .pdf $pdf) pdfcrop "${dir}/${base}.pdf" > /dev/null convert -density 600 "${dir}/${base}-crop.pdf" "${HOME}/Desktop/$base.png" > /dev/null done  Another one (that I just made after reading some of these) is a Python script that takes a list of TeX files and just does the same thing, but (obviously) compiles them with arara first. #!/usr/bin/env python from __future__ import print_function import os import sys import subprocess import shutil import tempfile import atexit if '--debug' in sys.argv: logmsg = lambda *a: print(*a, file=sys.stderr) else: logmsg = lambda *a: None logerr = logmsg DEVNULL = open(os.devnull, 'w') atexit.register(DEVNULL.close) def runcmd(*args): """Run a command with arguments""" logmsg('>>> Running %s' % ' '.join(args)) subprocess.check_call( ['/usr/bin/env'] + list(map(str, args)), stdout = DEVNULL) def compile(source): """Compile SOURCE and return the path to a PNG rendition of it.""" if not os.path.exists(source): logerr('%s does not exist' % source) return None oldsource = os.path.abspath(source) workdir = tempfile.mkdtemp() try: shutil.copy(source, workdir) os.chdir(workdir) basename = os.path.splitext(source)[0] pdfname = basename + '.pdf' pngname = basename + '.png' runcmd('arara', source) runcmd('pdfcrop', pdfname, pdfname) runcmd('convert', '-density', 300, pdfname, pngname) shutil.copy(pngname, os.path.dirname(source)) except subprocess.CalledProcessError: pngname = source finally: shutil.rmtree(workdir) return pngname for png_file in map(compile, sys.argv[1:]): print(png_file)  Again, piping the output to xargs <some imgur uploader> would work, but you wouldn't be using StackExchange's account. Regarding cruft files, I've developed a short yasnippet to help with that: # -*- mode: snippet; require-final-newline: nil -*- # name: sxtmp # key: sxtmp # -- % arara: pdflatex % arara: clean: { files: [  (let ((basename (file-name-base (or (buffer-file-name) "")))) (mapconcat (lambda (ext) (concat basename "." ext)) '("aux" "log" "out") ", ")) ] } \documentclass{article} \pagenumbering{gobble} \usepackage{mwe} \begin{document}$0
\end{document}

• +1 for multiple options. Why > /dev/null? And why not just use the /tmp directory? – musarithmia Feb 9 '15 at 3:33
• @AndrewCashner :) Well, I use both *nix and windows, so /tmp doesn't always exist. When I'm on *nix though, I do have an emacs function to create something in the /tmp directory (using make-temp-file), so I generally don't have to worry about cruft files. The first script would have to be edited to be in Batch when I'm on windows unless I have an implementation of bash there. – Sean Allred Feb 9 '15 at 12:26

On Debian GNU/Linux:

• Compile PDF (I do my examples in /tmp/, so /tmp/file.pdf)
• Open with mupdf or evince and magnify to desired size
• Use screenshot graphical application bundled with Gnome3 and select to take screenshot of selected area

• (In Gnome this can all be done by keyboard: press the spacebar, start typing screen, hit enter when icon appears.)
• Select the area, save as /tmp/file.png

My method to build MWE for TeX.SE in Linux:

1. Select the MWE of the question in the browser with the mouse (It is a bit annoying that in TeX.SE there are not a button to select the code, all the code and only the code).

2. Open Gummi with a new document and Ctrl-V to paste the MWE (why this simple editor? because it show a live preview, not the best for a long project, but ideal to test a MWE).

3. Solve the problem of the code (this is the longest part).

4. Ctrl-A, Ctrl-C (select all the code and copy to clipboard)

5. Run ksnapshot. This capture program can take a snapshot only of the preview window or a manually fixed rectangular area, so I will not need further processing of the image with other tools. I just have to press the "Save as ..." button. A new filename is proposed automatically, so I just have to accept it.

6. In the browser, Ctrl-V to paste the code and Ctrl+G, select the saved image and upload it.

All points, except the third, takes only a few seconds each.