summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/exsheets/exsheets-listings.sty
blob: 40d20de90cb47f413de976c8dfe8e74ef2199d64 (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
145
146
147
% ----------------------------------------------------------------------------
% the EXSHEETS-LISTINGS package
% 
%   Using listings with exsheets
% 
% ----------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/exsheets/
% E-Mail: contact@mychemistry.eu
% ----------------------------------------------------------------------------
% Copyright 2011-2019 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}
\ExplSyntaxOn
\ProvidesExplPackage{exsheets-listings}
  {\c_exsheets_date_tl}
  {\c_exsheets_version_tl}
  {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_sys_jobname_str - 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_sys_jobname_str - 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_sys_jobname_str - 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_sys_jobname_str - 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}

\file_input_stop:

HISTORY
2013/09/18 - first draft, see http://tex.stackexchange.com/a/133969/5049
2013/10/11 - first official version bundled with ExSheets
2017/02/08 - change deprecated \c_job_name_tl into the new \c_sys_jobname_str