# Flagging/tracing in roughdraft source document location

Surely I must be reinventing the wheel here.

I'm writing a rather lengthy document for which each section corresponds to its own input file. Basically, I have a mother document which looks something like the following:

%% Fellowship of the Ring
\input{preamble}
\begin{document}
\input{content/chap_01/opening}
\input{content/chap_01/announcement}
\input{content/chap_01/so_far_no_trouble}
\input{content/chap_01/twelve_more_years}
....
....


After writing a while, I no longer remember what all my file names mean. And when I find a mistake in the rough draft, it can be a real pain in the neck trying to pin down which file contains the offensive text.

A while ago, I hobbled some code together which allowed me to set a flag indicating that I was writing a rough draft and printed in the final document the source file (including the directory path). The code is really a horrendous hack in which I misuse \@startsection

Here's a MWE:

\documentclass{article}
%%--------------------------------------------------------------------------------
\usepackage{xcolor}
\usepackage{currfile}
\usepackage{fancyvrb}
\usepackage{xparse}
\makeatletter
\ExplSyntaxOn
%% booleans for flaggging ROUGH DRAFT status
\bool_new:N \ae_roughdraft_bool
\bool_gset_false:N \ae_roughdraft_bool
\NewDocumentCommand \setroughdraft   {} { \bool_gset_true:N  \ae_roughdraft_bool }
\NewDocumentCommand \unsetroughdraft {} { \bool_gset_false:N \ae_roughdraft_bool }
%%
\cs_new:Npn \fnc_ae_currentfile:n #1 {
\bool_if:NT \ae_roughdraft_bool
{
%% Save the filehandle
\DefineShortVerb{\;}
\SaveVerb{CurrentFilehandle};\currfiledir\currfilename;
\UndefineShortVerb{\;}
%% create a box of zero total height and zero width
\raisebox{-2ex}[0pt][0pt]{
\makebox[0pt][l]{
\footnotesize
\textcolor{red}{#1:\hspace*{1em}\UseVerb{CurrentFilehandle}}
}}
}
}

\cs_new:Nn \fnc_aesection:
{
\@startsection{section}{1}{0pt}
{2.5ex \@plus -0.5ex \@minus -0.5ex}
{2.5ex \@plus  0.5ex}
{\fnc_ae_currentfile:n {section}
\normalfont\Large\bfseries}
}

\NewDocumentCommand\aesection{ om }
{
\fnc_aesection: {#2}
}

\ExplSyntaxOff
\makeatother
\setroughdraft
%%--------------------------------------------------------------------------------
\usepackage{lipsum}
\pagestyle{empty}
\begin{document}

\input{hello}

\input{bye}

\end{document}


Here are the contents of the file hello.tex

\aesection{Hello}

\lipsum[1]


Here are the contents of the file bye.tex

\aesection{Bye}

\lipsum[2]


And here's the resulting document:

Bye unsetting the rough draft flag, the output is

Hopefully you can see how in a rough draft I can very easily tell which file produced what output and very quickly edit or modify that file.

Nevertheless, I'm not happy with the code. To begin with, I've got some undesired extra space popping in at the beginning of each section. But this is because I'm misusing \@startsection. I know I'm not using \@startsection correctly: only formatting commands should be appearing in the sixth argument---not actual text.

I would like to know a better way to do this. It seems to me that someone else must have wanted to do something similar.

How do I create a rough draft which allows me to readily tells the source file which generated the text in my output document?

-
Perhaps I am misunderstanding the question, but I would think that the "correct" approach here would be to use the --synctex=1 compile option. Then as you are reviewing your document when you see something that needs modifying you simply right click on the PDF and the corresponding .tex source file opens up. See for instance: Location inside a LaTeX source code. –  Peter Grill Apr 30 '13 at 23:41

You can keep the \section syntax, with *-version and optional argument.

\begin{filecontents}{elletthello.tex}
\section{Hello}

\lipsum[1]
\end{filecontents}
\begin{filecontents}{ellettbye.tex}
\section{Bye}

\lipsum[2]
\end{filecontents}

\documentclass{article}

\usepackage{xcolor}
\usepackage{currfile}
\usepackage{xparse}

%% keep the old meaning of \section
\let\latexsection\section

\ExplSyntaxOn
%% boolean for flaggging ROUGH DRAFT status
\bool_new:N \g_ae_roughdraft_bool
\bool_gset_false:N \g_ae_roughdraft_bool

\NewDocumentCommand \setroughdraft   {} { \bool_gset_true:N  \g_ae_roughdraft_bool }
\NewDocumentCommand \unsetroughdraft {} { \bool_gset_false:N \g_ae_roughdraft_bool }

\cs_new:Npn \ae_currentfile:n #1
{
\bool_if:NT \g_ae_roughdraft_bool
{
%% Save the filehandle
\tl_gset:Nx \g_ae_currentfile_tl {\currfiledir\currfilename}
%% create a box of zero total height and zero width
\raisebox{-1.5ex}[0pt][0pt]
{
\makebox[0pt][l]
{
\footnotesize\ttfamily
\color{red}#1:~\currfiledir\currfilename
}
}
}
}

\RenewDocumentCommand\section{ som }
{
\IfBooleanTF{#1}
{ \latexsection*{\ae_currentfile:n{section} #3} }
{
\IfNoValueTF{#2}
{
\latexsection[#3]{\ae_currentfile:n{section} #3}
}
{
\latexsection[#2]{\ae_currentfile:n{section} #3}
}
}
}

\ExplSyntaxOff

\setroughdraft

\usepackage{lipsum}

\pagestyle{empty}
\begin{document}

\input{elletthello}

\input{ellettbye}

\end{document}


(Have a look at the names of functions and variables.)

-
Nice solution. I thought \let doesn't work with commands that take optional arguments. Why is using \let here OK? –  A.Ellett May 1 '13 at 16:11

Your solution looks too complicated. Here is how I would do this in ConTeXt.

ConTeXt has a project-product-component model of splitting a big file into smaller files. So, each included file is a component and the macro \currentcomponent gives the name of the current component. Combining this with the usual method to put content after a section, we get:

\definehighlight
[displayfile]
[style=small, color=red]

[section]
[after={\setups{after:section}}]

\startsetups after:section
\startmode[roughdraft]
\blank[none]
\displayfile{section: \currentcomponent}
\stopmode
\blank[medium]
\stopsetups

\starttext

\component one
\component two

\stoptext


where the component files are one.tex

\startcomponent *

\startsection[title={One}]
\input lorem
\stopsection

\stopcomponent


and two.tex

\startcomponent *

\startsection[title={Two}]
\input lorem
\stopsection

\stopcomponent


To enable the roughdraft mode, compile the document using

context --mode=roughdraft filename


which gives

To disable the roughdraft mode, compile the document the usual way

context filename


which gives

-
Looks interesting. I've never used ConTeXt before so I'm not sure I understand too much of your code. –  A.Ellett Mar 31 '13 at 20:44