Home -> Projects -> How to Exit the Rat Race
When students go to career fairs, everyone around them tells them out to enter the rat race. I think this provides for an unbalanced experience, so at MIT’s upcoming 2020 xFair I intend to provide students with instructions on how to exit the rat race. It’s much safer to start something if you know how and when to stop!
pdf single double sided page distributed to students
ipython notebook used to generate the pdf
This could not possibly be an interesting topic, right? It’s not. But me bitching about PDF export tooling is entertaining, so here we go.
There are so many ways to turn an ipython notebook into a PDF. For starters, you can “print” the notebook webpage to a PDF. And you get a PDF out! Success? No. The PDF you get out looks terrible! A quarter of the page is taken up by the left margin, with no information content beyond telling you how many notebook cells you’ve read so far. Yuck. And furthermore the lines wrap differently than in the web view. Where code lines used to scroll, and you might have expected them to truncate, they instead wrap. I suppose I can see why that might be desirable, but I’m frustrated because I wanted some of those lines to truncate. I intentionally allowed boring content to go off screen because I didn’t want anyone to suffer through reading it. But now it’s back, and it’s keeping my PDF from fitting on a single page. Yuck! Turn it off! But that’s not the end of it – in addition to bad text wrapping there’s bad text non-wrapping! This is unsurprising at least, since it matches the interactive display, but some developer took the time to figure out how to make the contents of a pre tag wrap by character instead of wrapping by word or not wrapping at all. So some of my text needlessly has words split in half. So ugly.
But fortunately there are other options. One of them must be better, right? Not really. For starters, you can go to a print preview and then print. Honestly I don’t know if this produces a different result. I’d bet that it shouldn’t, but I’m not ready to bet that it doesn’t. Either way, not what I need.
You can export to latex from the command line, and there are flags for passing in custom latex templates. And some people on the internet have templates with macros to make pre text wrap in a sane way. But those custom templates have lots of hard coded sizes for things (such that you can’t reasonable change anything’s size and expect it to work). And it still doesn’t let me clip long code lines if I want to. Maybe good for future use cases, but not for this one.
I finally settled on Save As, manually editing css+html as needed to reformat, and print to PDF. This process is exactly as hideous as it sounds. But I designed this notebook to be printed, so it’s actually kind of nice to generate a PDF starting with the formatting I wrote it with and not some arbitrary transformation thereof, and for all that this method is not ok, it does do that. So what modifications are required?
Edit style.min.css:
.prompt -> display: none; (removes the enormous left margins).rendered_html pre -> margin: 1em 0; (indents plain text less than the default 2em)pre -> remove word-wrap and word-break lines (yeah, that mess)div#notebook -> font-size: 11px (reduce font size to better fit on a page).CodeMirror -> font-size: 92%.rendered_html pre -> font-size: 92% (monospace looks good at a smaller size than the rest of the text)pre -> font-size: 92%body, pre, output, div#notebook -> remove line-height (actually allow the document length to adapt to font size)div.cell.selected -> remove border-color (remove border of the active cell)div.cell.selected:before -> display:none (remove colored bar indicating the active cell)div.input_area-> margin-left: 2em (indent code blocks a little)div.output_subarea -> remove max-width (don’t prematurely wrap lines on code output)@media print -> remove background: transparent !important (allow background images)qr code “background” image
div.cell:first-child .rendered_html {
background: right 100% / contain no-repeat url("qr_code.gif");
-webkit-print-color-adjust: exact !important;
padding-right: 15em;
}div.output_area .MathJax_Display -> disable text-align override (center equations in the output)
Edit codemirror.css:
.CodeMirror-hscrollbar -> height: 0; (remove horizontal scrollbar. display: none doesn’t work because the display is changed inline).CodeMirror pre.CodeMirror-line -> remove font-size (don’t override font size – let me shrink it)Edit the html file, because codemirror uses too much inline css:
CodeMirror-sizer[^>]*min-height[^;]*; and disable min-height for any code box you want to shrinkpadding-bottom: 15px; and delete it (removes padding that makes space for a custom horizontal scrollbar)Finally open it in a browser and print to PDF. For some reason I got a blank page at the end, but that’s hardly something to complain about after everything else I’ve done – it’s easy to remove. I’ve finally got a PDF!
TODO. It hasn’t happened yet. Or I forgot to update this site. Or I decided not to incriminate myself by saying I’ve done something instead of just admitting to having plans ;)
But the basic idea is:
If step 1 falls through, then advertise outside the career fair, as close to the entrance as possible. Wish me luck!