15

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.

Tasks:

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.

lockstep
  • 250,273
Martin Scharrer
  • 262,582
  • 1
    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
  • 2
    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
  • @MartinScharrer I usually merge my dev branch to master everytime I'm ready to show the document to anyone. That way I can now what the latest submitted version is by looking for the latest merge into master. Also you can tag a commit (e.g. git tag foo) or write in the message commit that it's the version you submitted to be able to easily find them. – N.N. Jul 14 '11 at 18:35
  • @MartinScharrer, to echo and extend N.N.'s advice, I'm using the svninfo package in my thesis, which tags each page with the filename and revision number of the actively-being-processed file as each page is shipped out. (So if you have a short file \included, that may not show up in the page tags.) – Ben Lerner Jul 14 '11 at 18:42
  • @MartinScharrer, could you get most of the effects you want (except the paragraph->line numbers) by doing \g@addto@macro\ref{\marginpar{Ref: ##1}} and its equivalents for \cite and \label etc? – Ben Lerner Jul 14 '11 at 18:46
  • @BenLerner: I do similar things but using svn-multi (which I wrote) instead of svninfo. It's better to use one of the existing packages to do the cites and labels. – Martin Scharrer Jul 14 '11 at 18:51
  • @MartinScharrer, touche :) I know the packages I use, but haven't linked them back to their authors in my head... I suggested the add-to-macro solution above partly to clarify what effect you were after, and partly because if it truly is that short to implement, I tend not to include an extra package when I can do it myself that easily. (And, if it doesn't exist, then it makes for a great and tiny package itself :-p) – Ben Lerner Jul 14 '11 at 19:06
  • 2
    @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
  • @Martin And since you only need the approximate number, you could add \marginpar{\the\inputlineno} to a paragraph hook... – Seamus Jul 14 '11 at 21:44
  • @Martin So you want something like what is suggested here but with \the\inputlineno instead of a counter... – Seamus Jul 14 '11 at 21:52
  • @Martin: I think this question deserves the {draft} tag, so I had to remove another tag (and chose {printing}). – lockstep Jul 16 '11 at 16:43

3 Answers3

10

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.

Seamus
  • 73,242
  • 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
  • You need to use \precmdto (or so), because it has arguments. I tried now the lineno package and redefining the \thelinenumber macro to \the\inputlineno. It kind of works but shows the input line number of every end of every paragraph for each of its lines. It's a start. – Martin Scharrer Jul 16 '11 at 13:09
  • The command is \pretocmd. lineno has an option to only put a number every n lines... – Seamus Jul 16 '11 at 13:18
  • Floats are hard. But it's easy to patch \caption... – Seamus Jul 16 '11 at 13:42
  • Accepting suggestions for what to call this if I turn it into a package. extradraft? inputlines? – Seamus Jul 16 '11 at 15:17
3

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}

showkeys example

Tobi
  • 56,353
  • 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
  • @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
  • showkeys does work with biblatex. I just checked. (I don't know how well it works, but it doesn't break...) – Seamus Aug 12 '11 at 16:04
2

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.

You
  • 6,713
  • 2
  • 29
  • 27
  • 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 08 '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 08 '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 08 '11 at 14:17