Skip to content

Commit 6a0e08d

Browse files
committed
Add options to Lua examples pgf-tikz#640 pgf-tikz#839
1 parent bd8c9c4 commit 6a0e08d

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

tex/generic/pgf/graphdrawing/lua/pgf/gd/force/ControlElectric.lua

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@ declare {
3939
Two typical effects of increasing the |electric charge| are distortion
4040
of symmetries and an upscaling of the drawings.
4141
"]],
42-
examples = {[["
43-
\tikz \graph [spring electrical layout, horizontal=0 to 1]
44-
{ 0 [electric charge=1] -- subgraph C_n [n=10] };
45-
"]],[["
46-
\tikz \graph [spring electrical layout, horizontal=0 to 1]
47-
{ 0 [electric charge=5] -- subgraph C_n [n=10] };
48-
"]],[["
49-
\tikz \graph [spring electrical layout, horizontal=0 to 1]
50-
{ [clique] 1 [electric charge=5], 2, 3, 4 };
51-
"]]
42+
examples = {
43+
{
44+
options = [["preamble={\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{force}}"]],
45+
code = [["
46+
\tikz \graph [spring electrical layout, horizontal=0 to 1]
47+
{ 0 [electric charge=1] -- subgraph C_n [n=10] };
48+
"]]
49+
},{
50+
code = [["
51+
\tikz \graph [spring electrical layout, horizontal=0 to 1]
52+
{ 0 [electric charge=5] -- subgraph C_n [n=10] };
53+
"]]
54+
},{
55+
code = [["
56+
\tikz \graph [spring electrical layout, horizontal=0 to 1]
57+
{ [clique] 1 [electric charge=5], 2, 3, 4 };
58+
"]]
59+
}
5260
}
5361
}
5462

tex/generic/pgf/lua/pgf/manual/DocumentParser.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,17 @@ local function process_examples(t)
159159

160160
local n = {}
161161
for i=1,#t do
162-
n[i] = process_string(strip_quotes(t[i]))
162+
local code, options
163+
if type(t[i]) == "table" then
164+
code = assert(t[i].code)
165+
options = t[i].options
166+
else
167+
code = t[i]
168+
end
169+
n[i] = {
170+
options = process_string(strip_quotes(options)),
171+
code = process_string(strip_quotes(code))
172+
}
163173
end
164174
return n
165175
end
@@ -391,8 +401,9 @@ DocumentParser.addRenderer (
391401
print_on_output(output,
392402
"\\par\\smallskip\\emph{Example" .. (((#e>1) and "s") or "") .. "}\\par")
393403
for _,example in ipairs(e) do
394-
print_on_output(output, "\\begin{codeexample}[]")
395-
print_lines_on_output(output, example)
404+
local opts = table.concat(example.options or {}, "")
405+
print_on_output(output, "\\begin{codeexample}[" .. opts .. "]")
406+
print_lines_on_output(output, example.code)
396407
print_on_output(output, "\\end{codeexample}")
397408
end
398409
end
@@ -417,8 +428,12 @@ DocumentParser.addRenderer (
417428

418429

419430
function print_lines_on_output(output, lines)
420-
for _,l in ipairs(lines or {}) do
421-
output[#output+1] = l
431+
for n,l in ipairs(lines or {}) do
432+
if (n == 1 or n == #lines) and l == "" then
433+
-- skip leading and trailing blank lines
434+
else
435+
output[#output+1] = l
436+
end
422437
end
423438
end
424439

0 commit comments

Comments
 (0)