[prev in list] [next in list] [prev in thread] [next in thread]
List: lyx-users
Subject: alternate code scrap definition
From: "Sam Liddicott" <sam () liddicott ! com>
Date: 2009-07-28 14:17:29
Message-ID: 4A6F0879.8050808 () liddicott ! com
[Download RAW message or body]
This is a long post, but quite exciting to anyone who uses or wants to
improve literate programming with Lyx.
Along with using a literate module instead of a literate class
(attached), I've got a suggestion for an alternate code scrap definition.
Instead of using LatexType Paragraph with LabelType Static, (which
requires literal <<label>>= and @) I suggest these changes:
LatexType Environment
LatextName CodeScrap
LabelType Manual
It means that the first line which normally contains:
<<scrap-name>>=
will be the environment's label, shown in a different colour and we can
do without the << >>= because it will still have semantic meaning with
Lyx as the label.
So far, the Latex would come out like this:
\begin{CodeScrap}
[{scrap-name}]
int x=2;
int y=3;
\end{CodeScrap}
Which is not right, but some sed can turn:
\begin{CodeScrap}\n[{scrap-name}]
into
\n<<scrap-name>>=
and turn
\end{CodeScrap}
into
@
before we pass to notangle or noweave.
Such sed is:
sed -e '/\\begin{xdummy}/{s/^.*//;n;
s/^\[{/<</;s/}\] *$/>>=/;};/\\end{xdummy}/s/^.*/@\n/'
Further-more, we can use optional non-label text on the same line as the
label to contain a cross-reference definition such as would be used by:
\nextchunklabel{LABEL-NAME}
This means that to add a cross-reference label to a code chunk you would:
1. select "Scrap" from the drop-down
2. type in the scrap name
3. press space
3a. press space again and type the xref label
4. press ctrl enter and start typeing code.
just miss out step 3a if you don't want a cross-reference label.
This sed will do all of that (makes use of the hold space to detect the
CodeScrap environment):
sed -e '/\\begin{CodeScrap}/{h;s/^.*//};/\\end{CodeScrap}/s/^.*/@\n/;
/^\[{/{
x;/\\begin{CodeScrap}/{x
s/^\[{/<</;
s/^\(.* *}\]\) *\([^ ].*\)/\\nextchunklabel{\2}\n\1/
s/ *}\] */>>=/
x}
x
}'
It converted this Lyx (CodeScrap environment):
--8<-----8<-----8<-----8<-----8<---
something() start-of-something
int something() {
int x=2;
int y=3;
}
something_else
#warning I'm bored
--8<-----8<-----8<-----8<-----8<---
into this latex
--8<-----8<-----8<-----8<-----8<---
\nextchunklabel{start-of-something}
<<something()>>=
int something() {
int x=2;
int y=3;
}
<<something_else>>=
#warning I'm bored
@
--8<-----8<-----8<-----8<-----8<---
Which is spot on!
I wish that a module file could define the conversion filter. The
attached module file says:
OutputType literate
it would be good if there was also:
OutputFilter: sed -e ....
which defines a filter which is used to produce the specified output type.
Sam
["literate.module" (text/plain)]
#\DeclareLyXModule[noweb.sty]{Noweb}
#DescriptionBegin
#Adds \usepackage{noweb} to your document, along with the Scrap style.
#Also fixes the CharStyle:Code to work with noweb, redefining nowebs
#\code command to \nwcode (if you need it).
#DescriptionEnd
#use this filter on the output before passing to notangle or noweave:
# sed -e '/\\begin{CodeScrap}/{h;s/^.*//};/\\end{CodeScrap}/s/^.*/@\n/;/^\[{/{x;/\\begin{CodeScrap}/{x;s/^\[{/<</;s/^\(.* \
*}\]\) *\([^ ].*\)/\\nextchunklabel{\2}\n\1/;s/ *}\] */>>=/;x};x}'
Format 11
OutputType literate
AddToPreamble
\usepackage{noweb}
% noweb breaks code command
\newcommand{\nwcode}{\code}
\renewcommand{\code}[1]{\texttt{#1}}
EndPreamble
Style Scrap
Margin First_Dynamic
LatexType Environment
LatexName CodeScrap
NewLine 0
LeftMargin MMM
ParSep 0.4
TopSep 0.4
BottomSep 0.4
ItemSep 0.4
Align Left
AlignPossible Block,Left
FreeSpacing 1
PassThru 1
LabelType Manual
LabelFont
Color magenta
EndFont
TextFont
Color latex
Family Typewriter
EndFont
End
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic