Extended draft mode which includes source information for manual print-out to source synchronisation

I'm looking for a kind of extended draft mode which places the source code position of paragraphs, sectioning commands and figures etc. as marginpars or similar in the PDF, so that they appear on a print-out.

Background

I often give my paper drafts or (parts of) my thesis as PDFs to my coauthors/contributors and/or supervisors, respectively. They usually print it out and make corrections and remarks on paper. We tend to meet and I bring a Netbook (small Laptop) with me so that I can make the changes directly to the LaTeX code while we discus them.

The issue is that they make statements like "on the Xth line in the Yth paragraph on page Z it should be foo not bar" or "Figure X should be bigger". Now I have trouble converting that to the location in the LaTeX source code. I think I could use SyncTeX and have the PDF open, then click on the position they mention to get to the source (I never used it so far), but this doesn't work in this case anyway because normally I already made several changes on the source file since they printed the PDF out! Editing should be quickly and easily possible, so I can't start handling two different versions of source code and PDFs etc. At the moment I simply search for parts of the original text they mention, but this doesn't work all the time because this text might not be fully verbatim like that in the source file due to line breaks etc.

I like to get the following:

• Beside every sectioning title and start of paragraph (i.e. about one number per paragraph) I like to have the source line number displayed (e.g. as \marginpar)
• There where the code of a source file starts (\included chapters but also sections included using \input) I like to have the name of the source file printed.
• Figures should be marked with the name of the image files while the images are still displayed! If the figure is a diagrams (tikzpicture etc.) from an external file then the file name is also to be displayed. (Which actually might be already the case from the previous point).
• \labels and \refs should be displayed using the internal name (as \marginpars again)
• Bonus: Citiation keys (\cite) should be displayed like labels.

I know about packages which do display labels so this should be easily done but the rest needs some custom code I think.

-
Can LaTeX have access to the linenumbers of the file it's working from? If so, then that would be a start! – Seamus Jul 14 '11 at 17:23
@Seamus: Yes, I think so. The warning messages etc. print it. There is also some related code in the \begin macro. – Martin Scharrer Jul 14 '11 at 17:52
Another way to handle the problem might be to use version control and check out the version you gave to your collaborator as you meet and make changes to a branch of that version, then you merge the changes you made during the meet with the changes you made after sending the file to your collaborator. Of course, merging might result in conflicts that you have to resolve. – N.N. Jul 14 '11 at 18:23
@N.N.: I'm using version control. This is as good idea. – Martin Scharrer Jul 14 '11 at 18:27
@Seamus: The line number is given by the TeX primitive \inputlineno which is a number register. So \the\inputlineno would print it. – Martin Scharrer Jul 14 '11 at 21:25

The basic functionality of adding input line numbers to the start of each paragraph can be achieved like so:

\usepackage[excludeor]{everyhook}
\usepackage{marginnote}
\newif\ifnotmarginhook
\notmarginhooktrue
\PushPostHook{par}{%
\ifnotmarginhook
\notmarginhookfalse
\marginnote{\small\ttfamily\the\inputlineno}
\notmarginhooktrue
\fi
}

Which I shamelessly stole from TH's answer to a related question. On that question, people were saying that hooking into the paragraph like this is not a great idea because lots of unexpected things are built with paragraphs. So use at your own risk.

I guess it would be neat to add a "only put a line number every n paragraphs" option: that wouldn't be too much work, just a little counter and \ifnum magic...

I've started work on a package that builds on this answer. The github is here. It has a safemode option that doesn't do any paragraph hooking trickery so it should be safer. Instead it just patches particular environments and sectioning commands directly. This should be more robust if the above breaks. safemode currently doesn't work with titlesec because it looks like titlesec doesn't use \@startsection. (Or something weirder is happening...)

Using Martin Scharrer's own filehook and currfile packages, you can signpost where inputted files start and finish like so:

\usepackage{filehook,marginnote}
\AtBeginOfInputs{%
\@reversemargintrue
\notmarginhookfalse
\marginnote{\ttfamily\currfilename}
\@reversemarginfalse
\notmarginhooktrue
}

\AtEndOfInputs{%
\@reversemargintrue%
\notmarginhookfalse
\marginnote{\ttfamily End \currfilename}%
\@reversemarginfalse%
\notmarginhooktrue
}

The \@reversemargintrue is just to make the notes appear on the opposite side (so that they don't overlap with line numbers). Same thing works for \include but you might want to handle that slightly differently just because of the forced pagebreaks. The package I'm working on allows you to put the current file name in the header and/or footer as well.

As mentioned in another answer, the showkeys package can deal with the last two points.

Finally, adding the file name of included graphics. The following seems to work in my not very thorough testing. I don't know if this is at all the appropriate way to do it: I just searched graphics.sty for draft and bodged from there.

\usepackage{graphicx,marginnote,etoolbox}
\apptocmd\Gin@setfile{%
\@reversemargintrue%
\notmarginhookfalse%
\marginnote{\ttfamily #3}%
\@reversemarginfalse%
\notmarginhooktrue%
}{}{}

I hope to have a version of the package on CTAN in a couple of weeks once I've done a little more testing, written some proper documentation and learned about .dtx files.

-
 I tried testing this with \Blinddocument but of course, every linenumber is the same! – Seamus Jul 16 '11 at 12:31 Damn. \appto{\list}{\addlineno} doesn't work. – Seamus Jul 16 '11 at 12:35 Thanks. Adding the line number macro to the front of \@startsection which is used by all section and paragraph macros (but not chapters) should be enough. – Martin Scharrer♦ Jul 16 '11 at 12:38 Ooh. I've made it better. But I have to be careful how many times I edit this before it automagically becomes CW – Seamus Jul 16 '11 at 12:39 @Martin \preto{\@startsection}{\addlineno} seems to break... – Seamus Jul 16 '11 at 12:41

Do you know the showkeys package, which provides solutions for your last two points?

\documentclass{article}
\usepackage{showkeys}

\begin{document}
\section{section name}\label{sec1}
Some text \dots
\section{section name}
As seen in section~\ref{sec1} or in \cite{bib}

\begin{thebibliography}{1}
\bibitem{bib} Some bib entry
\end{thebibliography}
\end{document}

-
 As stated, I know that such packages exists. I'm looking for something which gives me also the rest. This of course requires a combination of packages and extra code I think. – Martin Scharrer♦ Jul 14 '11 at 17:24 Does it do citation as well? – Martin Scharrer♦ Jul 14 '11 at 17:25 @Martin: The manual says it does … and sorry for overlooking that you said your’re knowing some packages … – Tobi Jul 14 '11 at 17:35 Thanks for the example and the clarification. – Martin Scharrer♦ Jul 14 '11 at 17:51 @Martin: To mak sure that I was right about the cites I extended my example. And it works as expected - but I don’t know if it works together with biblatex. – Tobi Jul 14 '11 at 18:19
show 1 more comment

For your first point, I recently (as in this evening) wrote a very small package doing just that (with a little help from marginnote and everyhook). It's called srcline and is available from Bitbucket. Unfortunately it doesn't typeset numbers inside floats or lists yet, as that seems to mess up the document layout quite a bit, but it may be a good starting point.

Edit: I just realized that this is extremely similar to the solution/package proposed by Seamus.

-
 If you use \marginnote from the marginnote package, you should be able to get margin notes from inside floats. They won't float, but that shouldn't be a problem. – Seamus Aug 8 '11 at 11:30 If you tried the same thing I did with the everyhook package, it will probably break \paragraphs... Maybe we should collaborate on a package? – Seamus Aug 8 '11 at 11:35 @Seamus: I am using \marginnote. The problem was that with margin notes inside floats the layout changed too much compared to when not using them, which I felt was kind of purpose-defeating as the use case is being able to refer to a source code line while proofing the document. Re collaboration, I'm not sure. This was mostly a quick hack for myself and I don't really intend to extend it too much as I won't have time to do so. – You Aug 8 '11 at 14:17