Scripted copy paste in LaTeX – A (dirty?) content reuse trick

I had a report to write, and in that report I had sections on topics, each of which had action points. And it would have been useful to have one big fat list of all the action points in one place, rather than all over the sections. My boss specifically requested this, as it would make his life easier post-report. But I didn’t want to copy-paste them by hand, because that would mean having duplicate content to maintain.

illustration of copy paste across documents or sections

It took me a while to figure this out, but I finally found a solution!! There is a package in LaTeX called clipboard, and I used it to mark out sections to copy out, and where to paste them in!

So while I only wrote the content once, in one place (actions nested within the sections they belonged to), it appeared in two places!! No duplication! No manual labour! No pain!

It did have the side effect of removing the subsection and subsubsection headings from the table of contents of the PDF (weird!!), but given how much pain and effort it saved me, I am going to live with this.

The package

There is a lovely package called clipboard, which you can use to script copying and pasting across a LaTeX document.

How to install it

You need to grab the clipboard.sty file (you can right click and save the previous link) and save it in a directory where LaTeX will find it. In my case, this was /Library/TeX/Root which seems to have actually placed it in /usr/local/texlive/2016.

Then it’s best to restart your LaTeX editor of choice.

How to use it (well, how *I* used it)

Full instructions on how to use it are in clipboard.pdf by Eduardo C. Lourenço de Lima.

I personally did the following.

Firstly, I instantiated a clipboard. This is especially useful as you can access the clipboard(s) from any LaTeX document (OMG, copy-pasting, in script, across documents!!!).

\newclipboard {myActionItems}

For every big chunk of content (three subsubsections) that I needed to reproduce in another place in the document, I chose a “key”.

You save content to your clipboard using “keys”, meaning you can then replace each piece of content where you want, using its key. Yes, you can have many pieces copied at once, with a key each! It’s like a mini-database…

And I surrounded each content block with

\subsection{Action items}
\Copy {key1} { % --- START COPY (this is a comment to find this line again)
MY CONTENT WAS HERE
} % --- END COPY (this is a comment so I don't misplace this curly brace)
\Paste {key1}

Note I made sure to paste it immediately after I’d copied it, so it would show up (!!).

And then I went to the other part of my document where I needed those three subsubsections, and did this.

\subsection{Action items for key1}
\Paste {key1}

What it does

If you’ve never used LaTeX before, then you may never have heard of typesetting. I think of it as “compiling”, the way that a compiler transforms your programming source code into machine-legible executable code… Typesetting is a step that transforms your markup and content into PDF-formatted information, thereby creating a document that can be read and printed.

How this clipboard package works is that it runs the copy and paste commands during this typesetting “compiling” step, and executes your copy-paste instructions as you have defined them. So you can display the content that you define once in as many places as you need.

There are better ways to do this

This is not the best way to reuse content in LaTeX. In fact, the best way is to have separate documents which you bring in to a main one. However, I was putting together a single report, not a thesis, and I had not had the presence of mind to do that. Nor did I have the time, once I got going, to set myself up with a well thought out file architecture. I’m too obsessed with information architecture to wing it!

Given this worked a treat, I also decided to quickly share it. Because it took me HOURS of googling to finally locate this package.

There is extensive information on how to fragment documents, include them, merge them, etc… but not a single blog post about how to do this dirty little content reuse trick!! So I had to write one, because when you’re still a LaTeX newbie (as I am), this is a very useful thing to know how to do!!

If anyone find more information or posts about this package, please could you also share the links here.