summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/exsheets/exsheets-listings.sty
blob: 8ae6f4ffdc597bd5f5d32250f0d5ee24434bfa49 (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
140
141
142
143
144
% ----------------------------------------------------------------------------
% the EXSHEETS-LISTINGS package
% 
%   Using listings with exsheets
% 
% ----------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/exsheets/
% E-Mail: contact@mychemistry.eu
% ----------------------------------------------------------------------------
% Copyright 2011-2014 Clemens Niederberger
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% ----------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% ----------------------------------------------------------------------------
\RequirePackage{exsheets,listings}
% we need this loaded before expl3 syntax is activated:
\lst@RequireAspects{writefile}
\ProvidesExplPackage{exsheets-listings}
  {\exsheets@date}
  {\exsheets@version}
  {listings in exsheets}

% ----------------------------------------------------------------------------
% variables:
\int_new:N \g__exsheets_listings_question_int
\int_new:N \g__exsheets_listings_solution_int

\tl_new:N  \l__exsheets_listings_question_pre_tl
\tl_new:N  \l__exsheets_listings_question_post_tl
\tl_new:N  \l__exsheets_listings_question_options_tl
\tl_new:N  \l__exsheets_listings_question_code_tl
\tl_new:N  \l__exsheets_listings_solution_pre_tl
\tl_new:N  \l__exsheets_listings_solution_post_tl
\tl_new:N  \l__exsheets_listings_solution_options_tl
\tl_new:N  \l__exsheets_listings_solution_code_tl
\tl_new:N  \l__exsheets_listings_question_points_tl

% ----------------------------------------------------------------------------
% listings style for questions and solutions:
\keys_define:nn { exsheets }
  {
    question / listings .tl_set:N = \l__exsheets_listings_question_code_tl ,
    solution / listings .tl_set:N = \l__exsheets_listings_solution_code_tl
  }

% ----------------------------------------------------------------------------
% expansion help:
\cs_new:Npn \__exsheets_question_expandopt:Nnnnn #1#2#3#4#5
  { #1{#2}[#3]{#4}#5 }
\cs_generate_variant:Nn \__exsheets_question_expandopt:Nnnnn { NnVox }

\cs_new:Npn \__exsheets_question_expand_opt_and_body:Nnnn #1#2#3#4
  { #1{#2}[#3]#4 }
\cs_generate_variant:Nn \__exsheets_question_expand_opt_and_body:Nnnn { NnVx }

% ----------------------------------------------------------------------------
% lstquestion environment:
\keys_define:nn { exsheets / listings / question }
  {
    pre      .tl_set:N = \l__exsheets_listings_question_pre_tl ,
    post     .tl_set:N = \l__exsheets_listings_question_post_tl ,
    options  .tl_set:N = \l__exsheets_listings_question_options_tl ,
    points   .tl_set:N = \l__exsheets_listings_question_points_tl ,
    listings .tl_set:N = \l__exsheets_listings_question_code_tl ,
  }

\keys_define:nn { exsheets / listings / solution }
  {
    pre      .tl_set:N = \l__exsheets_listings_solution_pre_tl ,
    post     .tl_set:N = \l__exsheets_listings_solution_post_tl ,
    options  .tl_set:N = \l__exsheets_listings_solution_options_tl ,
    listings .tl_set:N = \l__exsheets_listings_solution_code_tl ,
  }

\cs_new:Npn \exsheets_listings_new_lstenvironment:nnnnnnn #1#2#3#4#5#6#7
  {
    \lstnewenvironment{#2}[1][]
      {
        \keys_set:nn {exsheets/listings/question} {#1,#4,##1}
        \int_gincr:N \g__exsheets_listings_question_int
        \lst@BeginWriteFile
          { \c_job_name_tl - ex \int_use:N\g__exsheets_listings_question_int .lst }
      }
      {
        \lst@EndWriteFile
        \__exsheets_question_expandopt:NnVox
          \begin {#3}
          \l__exsheets_listings_question_options_tl
          { \l__exsheets_listings_question_points_tl }
          {
            \exp_not:V \l__exsheets_listings_question_pre_tl
            \exp_not:N \lstinputlisting
              [ \exp_not:V \l__exsheets_listings_question_code_tl ]
              { \c_job_name_tl - ex \int_use:N\g__exsheets_listings_question_int .lst }
            \exp_not:V \l__exsheets_listings_question_post_tl
          }
        \end {#3}
      }
    \lstnewenvironment{#5}[1][]
      {
        \keys_set:nn { exsheets / listings / solution } { #1,#7,##1 }
        \int_gincr:N \g__exsheets_listings_solution_int
        \lst@BeginWriteFile
          { \c_job_name_tl - sol \int_use:N \g__exsheets_listings_solution_int . lst }
      }
      {
        \lst@EndWriteFile
        \__exsheets_question_expand_opt_and_body:NnVx
          \begin {#6}
          \l__exsheets_listings_solution_options_tl
          {
            \exp_not:V \l__exsheets_listings_solution_pre_tl
            \exp_not:N \lstinputlisting
              [ \exp_not:V \l__exsheets_listings_solution_code_tl ]
              { \c_job_name_tl - sol \int_use:N \g__exsheets_listings_solution_int . lst }
            \exp_not:V \l__exsheets_listings_solution_post_tl
          }
        \end {#6}
      }
  }

\NewDocumentCommand \NewLstQuSolPair { O{}mmO{}mmO{} }
  { \exsheets_listings_new_lstenvironment:nnnnnnn {#1}{#2}{#3}{#4}{#5}{#6}{#7} }

\NewLstQuSolPair{lstquestion}{question}{lstsolution}{solution}

\tex_endinput:D

HISTORY
2013/09/18 v0.1 - first draft, see http://tex.stackexchange.com/a/133969/5049
2013/10/11 v0.2 - first official version bundled with ExSheets