3

I have a pretty complicated command, lets call it "C" in the preamble a long document that changes how the document looks.

If I'm writing text somewhere in the middle of the document, I'd like to avoid always scrolling to the top, uncommenting the command, and scrolling back and then compiling, to see how the document looks, when "C" is enabled and then undoing everything to see how it looks like with "C" disabled. I'd like a faster workaround for this.

The following would be helpful: Is it possible define a new, short command, like \activate that I can place anywhere in the body of the LaTeX document, that will somehow activate the command from the preamble, so that I can just quickly type \activate when I'm writing text in the middle of the document, then compile and then delete \activate again to see how it looks like without it.
This already would save me a LOT of time that presently I'm spending scrolling around my document.

6
  • 2
    You could wrap the definition of your command "C" inside a conditional \ifC (which you would define by \newif\ifC), i.e. \ifC <definition text> \fi and then enable/disable it via \Ctrue and \Cfalse. (Of course you could define a shortcut like \def\activate{\Ctrue}). – Ruben Aug 28 '15 at 13:02
  • 5
    impossible to say in this generality. if your command is called \C why not just issue \C at the point you need it? If it only works at the start because it sets global document settings, then hard to see how you can activate it mid document – David Carlisle Aug 28 '15 at 13:02
  • You may also show what "C" is exactly doing. – Ruben Aug 28 '15 at 13:45
  • 1
    @DavidCarlisle, your suggestion is quite helpful I'd say (because it is clearly most efficent), but it alredy fails if "C" sets the parindent as in my solution. – Ruben Aug 28 '15 at 16:32
  • 1
    @Ruben If your guess of what the OP meant is right, although it's hard to see why anyone would want a command in the middle of the document to affect the indentation of all earlier paragraphs. I'd rather the question was clarified before attempting an answer:-) – David Carlisle Aug 28 '15 at 16:40
3

Here is a solution that should cover most of the cases.

First, I defined \C to simply set the \parindent to 0 for testing purposes. Then the basic idea is to delay the execution of \C until the begin of the document using the standard interface command \AtBeginDocument. Inside of it the conditional testing is done. When \ifC is set to true it will actually execute \C and relax if false. Now, you want to control the execution mid-document. That's why \activate will write \Ctrue to the main auxiliary file. (It gets loaded before the begin-document hook is called!) From that follows that you need to compile twice to see the changes.

\documentclass{article}
\usepackage{lipsum}

\def\C{\parindent0pt}

\newif\ifC
\AtBeginDocument{\ifC\C\fi}
\makeatletter
\def\activate{%
  \@bsphack
  \if@filesw
    \immediate\write\@mainaux{\string\global\string\Ctrue}\fi
  \@esphack
}
\makeatother

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\activate

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 
\end{document}

To automate the process of compiling your document twice you could use arara. It is very handy and easy to use:

% arara: pdflatex
% arara: pdflatex

\documentclass{article}
\usepackage{lipsum}

\def\C{\parindent0pt}

...

You would compile this file through arara filename.tex instead of pdflatex filename.tex. To learn more about it you can have a look at its github repository or its CTAN entry and you can download an installer for the latest version (v3.0).

5
  • Do you really think, that OP, who assumes that scrolling to the top of a document is the waste of time, will be satisfied with the solution, which requires double compilation? – Przemysław Scherwentke Aug 28 '15 at 13:48
  • @PrzemysławScherwentke -- Not really easy to answer. It depends on where the function of the compilation time and the function of scrolling-to-top time intersect (if they do at all), hence it depends on the exact length of the document. I'd say compiling twice still should be faster as the document may be large enough that scrolling down again to the point where the impact of "C" should be tested is rather tedious. And consider the time needed uncommenting the whole definition... – Ruben Aug 28 '15 at 16:14
  • @Ruben This indeed seems a great solution, if it weren't for the necessity of compiling twice - but you were right, this does save me a least some time, as scrolling takes longer than compiling twice. Asking for perfection: Would there be a way though to automate compiling twice ? – l7ll7 Aug 30 '15 at 14:23
  • @user10324, happy to hear this! In fact there are possibilities to automate compilation cycles. There is a neat tool called arara that replaces latexmk which was designed for exactly this task. Former one (which I recommend) is used through directives specified in the main manuscript file, e.g. % arara: pdflatex. In your case you would just use this line twice to have automatic double compilation. You can find information about it in @PauloCereda's github repo: github.com/cereda/arara – Ruben Aug 30 '15 at 21:13
  • @Ruben you're great! and your solution is ultimately the best. (a funny coincidence: on a different question of mine, tex.stackexchange.com/questions/263621/…, another user yesterday also recommended arara) – l7ll7 Sep 1 '15 at 14:15
0
\newif\ifC
\def\C{\ifC{your definition of C} \else \fi}

And now you activate your command setting \Ctrue and desactivate, setting \Cfalse.

3
  • This is exactly my comment and it does not answer the question yet, as further information is needed in the OP... And why the group around "your definition of C"? Worst case it will localize some assigmnets that are needed outside the group. – Ruben Aug 28 '15 at 13:06
  • @Ruben We may assume that known from Chapter 20 of The TeXbook \ifabc (and how to call it when the command in mind is \C?) is not known to OP (I have scrooled through his/her questions). In this sense it is an answer to the question. Certainly, I can imagine command, for which this attempt doesn't work, but assuming it is premature. – Przemysław Scherwentke Aug 28 '15 at 13:17
  • Your solution will fail prety fast and it does not cover the requirement that \C may be activated mid-document. It does not answer the question. – Ruben Aug 28 '15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.