summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/includernw/includeRnw.sty
blob: 14cecb5b9c1cc71d6253927aeed95559138ae099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
% Source (with comments) can be found at https://github.com/Strauman/includeRnw/
%% The LaTeX package includeRnw - version v0.0.2 (2018/04/29) - build 10
%% includeRnw.sty
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2018 by Andreas Storvik Strauman <andreas dot s dot strauman at uit dot no>
%% -------------------------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008/05/04 or later.
%% This work has the LPPL maintenance status `author-maintained'.
%% This work consists of all files listed in README
\ProvidesPackage{includeRnw}[2018/04/29 v0.0.2 Makes commands for including external .Rnw files.]
\providecommand\rnw@loglevel{0}
\RequirePackage{pdftexcmds}
\RequirePackage{kvoptions}
\let\incl\input
\RequirePackage{kvoptions}
\SetupKeyvalOptions{ family=includeRnw,
 prefix=rnw@opt@
}
\DeclareBoolOption[true]{build}
\DeclareComplementaryOption{halt}{build}
\DeclareBoolOption[false]{new}
\DeclareBoolOption[true]{knithead}
\DeclareComplementaryOption{noknithead}{knithead}
\ProcessKeyvalOptions*\relax
\ifrnw@opt@build\else
  \ifrnw@opt@new
    \@latex@warning{includeRnw: can't use halt and new together. Falling back to new.}
    \rnw@opt@haltfalse
  \fi
\fi
\providecommand\rnw@dir@input{.}
\providecommand\rnw@dir@output{./knitrout}
\providecommand\rnw@suffix{knitted}
\providecommand\rnw@file@knitlog{\rnw@dir@output/knitlog.log}
\providecommand\rnw@filebase@knithead{\rnw@dir@output/knithead}
\def\insp#1{\texttt{\string#1:\meaning#1}}
\def\inspw#1{\@latex@warning{\string#1:\meaning#1}}
\providecommand\rnw@loglevel{3}
\newcommand\@dlog[2][1]{\ifnum\rnw@loglevel>#1\relax\@latex@warning{#2}\fi}
\def\rnw@filename@parse#1{%
 \filename@parse{#1}
 \edef\rnw@filepath{\filename@area\filename@base}
 \edef\rnw@filebase{\filename@base}
 \edef\rnw@fileext{\ifx\filename@ext\relax Rnw\else\filename@ext\fi}
 \edef\rnw@infile@fullpath{\filename@area\filename@base.\filename@ext}
}
\def\rnw@clear@knitlog{%
\immediate\write18{echo "" > \rnw@file@knitlog}
}
\def\rnw@check@ouput@file{\rnw@dir@output/.includeRnwShellEscapeCheck}
\def\check@shell@escape{%
  \ifcase\pdf@shellescape%
  \PackageError{includeRnw}{\string\includeRnw\space requires --shell-escape}{}\stop\or%
  \message{Shell escape test passed}\or%
  \PackageError{includeRnw}{\string\includeRnw\space requires --shell-escape. Current is restricted shell escape}{}\stop\fi%
}
\def\check@output@dir{%
  \immediate\write18{touch \rnw@check@ouput@file}
  \IfFileExists{\rnw@check@ouput@file}{}{%
    \immediate\write18{mkdir \rnw@dir@output}
}
  \immediate\write18{rm \rnw@check@ouput@file}
}
\def\rnw@include@knithead{%
  \IfFileExists{\rnw@filebase@knithead.tex}{}{%
    \@dlog[0]{Creating knithead}
    \immediate\write18{echo "<<create-preamble,echo=FALSE,results='asis'>>=\string\ncat(knitr:::make_header_latex())\string\n@" > \rnw@filebase@knithead.Rnw}
    \immediate\write18{R -e 'library("knitr");knit("\rnw@filebase@knithead.Rnw","\rnw@filebase@knithead.tex")' >> \rnw@file@knitlog}
}
  \IfFileExists{\rnw@filebase@knithead.tex}{\@dlog[0]{Including knithead}\incl{\rnw@dir@output/knithead.tex}}{\PackageError{includeRnw}{Could not find knitr preamble: \rnw@dir@output/knithead.tex}{}}
}
\xdef\rnw@gopt@halt{h}
\xdef\rnw@gopt@force{f}
\def\ifrnw@should@knitr{  \@dlog[0]{Deciding whether to do knitting}
  \let\ifrnw@local@build\ifrnw@opt@build
  \newif\ifrnw@doknit
  \rnw@doknittrue
  \@dlog[2]{Decision is given option: \givenopt}
  \ifnum\pdfstrcmp{\rnw@gopt@force}{\givenopt\relax}=\z@\relax\rnw@doknittrue\@dlog[2]{Focing build}\else%
    \ifnum\pdfstrcmp{\rnw@gopt@halt}{\givenopt\relax}=\z@\rnw@doknitfalse\relax\else%
        \ifrnw@opt@new%
        \else%
          \let\ifrnw@doknit\ifrnw@opt@build\relax%
        \fi%
    \fi
  \fi
\ifrnw@doknit
\@dlog[1]{Decided to build}
}
\def\rnw@purge@outdir{  \immediate\write18{rm -rf \rnw@dir@output}
  \immediate\write18{mkdir \rnw@dir@output}
}
\providecommand\rnw@execute@knitr[2][]{%
  \def\givenopt{#1}
  \rnw@filename@parse{#2}
  \xdef\knitOutfile{\rnw@dir@output/\filename@base\rnw@suffix.tex}
  \ifrnw@should@knitr
    \IfFileExists{\rnw@infile@fullpath}{      \@dlog[2]{Building \rnw@infile@fullpath\space to \rnw@dir@output/\filename@base\rnw@suffix.tex}
      \immediate\write18{R -e 'library("knitr");knit("\rnw@dir@input/\rnw@infile@fullpath",  "\rnw@dir@output/\filename@base\rnw@suffix.tex")' &> \rnw@file@knitlog}
}{\@latex@error{includeRnw: Could not find file that I was asked to knit: \rnw@infile@fullpath!}{}\stop}
  \else \@dlog[2]{- Skipping knit of \rnw@infile@fullpath}
  \fi
  \IfFileExists{\knitOutfile}{%
    \@dlog[2]{Found \knitOutfile. Including it.}
    \incl{\knitOutfile}
}{%
    \PackageError{includeRnw}{Couldn't find knitted file: \knitOutfile}{}
}
}
\newcommand\rnw@settable[3][]{%
\edef\reserved@R{\expandafter\@gobble\string #2}%
\@ifundefined\reserved@R%
  {%
    \gdef#2##1{\gdef#3{##1}#1}
}%
  {%
    \@ifundefined{no\reserved@R}{      \@latex@error{includeRnw: Tried to define \@backslashchar \reserved@R \space, but it's already defined. \MessageBreak If you don't need the command \@backslashchar \reserved@R \space, then just define \string\no\reserved@R\space before you include the includeRnw package: \string\def\string\no\reserved@R{}}{}\stop
}{      \@latex@warning{\@backslashchar\reserved@R is not defined by includeRnw since it was already defined someewhere else. Continuing since \string\no\reserved@R is defined.}
}
}%
}
\rnw@settable\rnwInputDirectory\rnw@dir@input
\rnw@settable\rnwKnittedSuffix\rnw@suffix
\rnw@settable\rnwKnitlogFile\rnw@file@knitlog
\rnw@settable\rnwKnitheadName\rnw@filebase@knithead
\let\includeRnw\rnw@execute@knitr
\let\purgeOutDir\rnw@purge@outdir
\check@shell@escape
\check@output@dir
\rnw@clear@knitlog
\ifrnw@opt@knithead%
  \rnw@include@knithead
\fi