Replies: 3 comments 1 reply
-
When wanting to display non-executable code, I have also used and seen others just using I guess just to clarify: this particular usage is aimed at people wanting to share a block of code that is not linked to any data (?). Say, maybe a chunk of pseudocode to describe an algorithm, or even language-specific code to just illustrate how a procedure is done (rather than to produce a figure or plot). As for executable code chunks, it might be also possible to define the global programming language at the preamble of a LaTeX file (which could be genererically named .nw). Whenever a second language is used, then the use specifies it in the |
Beta Was this translation helpful? Give feedback.
-
A little more googling lead me to python.sty circa 2008 which adds a \documentclass{article}
\usepackage{graphicx}
\usepackage{python}
\begin{document}
\begin{figure}
\centering
\begin{python}
#
from pyx import *
g = graph.graphxy(width=8)
g.plot(graph.data.function("y(x)=sin(x)/x", min=-15, max=15))
g.writePDFfile("function")
print r'\includegraphics{function}'
\end{python}
\caption{$y(x)=\frac{\sin(x)}{x}$}
\end{figure}
\end{document} but more importantly the well documented, and actively developed and distributed
that adds the following inline commands:
and the following LaTeX environments
Given this prior art, we should support at least some of these (although some variants are nor applicable / redundant given Stencila's execution semantics or can be accommodated via options). For example, |
Beta Was this translation helpful? Give feedback.
-
LuaTeX's syntax for embedded Lua might also be worth a look $2 + 5 \neq \ctxlua{context(3+5)}$, but is equal to \ctxlua{context(2+5)}.
This is \ctxlua{context(string.upper("absolutely"))} true.
\startluacode
-- include the file my-lua-lib.lua
require("my-lua-lib")
\stopluacode |
Beta Was this translation helpful? Give feedback.
-
Over in Encoda @fkmendes and I are working on improving Stencila's support for LaTeX. We have had a LaTeX converter for some time (based on Pandoc) but it had had little attention.
One of the things that we are, of course, wanting to have in LaTeX documents is executable code. So this is just to kick start some discussion on that. In particular, I'm keen to get the input of people like @finlay, @edwardabraham and @richardmansfield (apolgies for the poke :) who have been using LaTeX for executable documents for a long time. I'd love to know what conventions / syntax you all use for executable code within LaTeX.
Using (abusing?) existing LaTeX commands
I've created a text fixture in that PR who's main purpose is to see how we can represent various types of
Code
nodes from Stencila's Schema in LaTeX (i.e what LaTeX command Pandoc parsers and how it parses them).See https://github.com/stencila/schema/tree/master/schema for the definitions of Stencila
Code
nodes. See https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/Readers/LaTeX.hs for how Pandoc parses LaTeX.I have looked at the Pandoc JSON output from parsing all the following examples and, based on that, think we can use all of the following LaTeX encodings.
A
CodeFragment
with noprogrammingLanguage
can be represented using\verb
.A
CodeFragment
with aprogrammingLanguage
can be represented using\lstinline
A
CodeBlock
with noprogrammingLanguage
can be represented using averbatim
environment.A
CodeBlock
with aprogrammingLanguage
can be represented using alstlisting
environment.The above are all "static" code nodes with standard approaches for inclusion in LaTeX. The following a "dynamic" code nodes - where we want to find was to represent them in LaTeX using modifications of syntaxes that Pandoc already knows about.
A
CodeExpression
could be represented by addingexec
to the\lstinline
language. (It would be preferable to use a comma between the language and the "exec" (i.e. a different "attr" on the element) but at the time of writing Pandoc v2.12 only seems to passlanguage
and not other attributes.)A
CodeChunk
could be represented by adding theexec
class to\lstlisting
language. In this case, Pandoc v2.12 does parse "exec" as a separate attribute on the element (in contrast tolstinline
) but for consistency we don't use the comma here.A
CodeChunk
withid
andcaption
properties can be represented using a lstlisting as well (but might require a second parsing of the caption string)Using existing syntax for executable code in LaTeX
noweb
is a very old, but seemingly not widely used, syntax for literate programming allowing embedding executable code within LaTeX:A
CodeChunk
is represented innoweb
like this:A
CodeExpression
is equivalent to a "Mini-code chunk" (in Sweave parlance) and designated by\Sexpr{}
(for S expression, S being R's precursor of course):\expr{}
command.\expr[language=python]
rnw
,pynw,
jsnw`nw
"codec" that would parse thenoweb
syntax and convert it into pure LaTeX as defined in the previous section, which would then be parsed by the existinglatex
codec for conversion to Stencila Schema. This is analogous to how ourxmd
codec (for Rmarkdown-like files) works.Beta Was this translation helpful? Give feedback.
All reactions