> It seems this could be helpful for me too. I just didn't understand so
> far how to do a thing of this kind. .-(
> So, would you mind to post this lyx-shortcut (and the latex commands)
> here? Thanks
I post all I have done.
There are still some undetected bugs, but it works on and decent *niix
machine. cygwin, I don't know.
=====================================
In .lyx/layouts/Mystuff.inc
====================================
# Cpp code layout
Style GGGcpp
LatexType Environment
LatexName KODcpp
ParSkip 0.4
ParSep 0.4
TopSep 0.4
Align Left
AlignPossible Left
LabelType Top_Environment
LabelString "C++ kod"
OptionalArgs 1
# label font definition
LabelFont
Series Bold
EndFont
End
=====================================
In .lyx/external_templates
========================================
Template CppKod
GuiName "[C++ $$Basename]"
HelpText
Snyggar till C++ kod
HelpTextEnd
FileFilter "*"
ViewCommand "nedit $$FName"
EditCommand "nedit $$FName"
AutomaticProduction true
Format LaTeX
Product "{\\small \\input{$$FName.tex} }"
UpdateCommand "highlight -A -L -t 4 -I -f -r -q $$FName -o
$$FNa
me.tex"
UpdateResult "$$FName.tex"
FormatEnd
TemplateEnd
=========================================
In .lyx/bin/Mystuff.bind
\bind "M-g p c" "layout GGGcpp"
================================0
The C++ code is attached at the end.
To use all of this, I run the command
lyx -e latex PC.lyx && LyX2LaTeX2highlight2LaTeX PC.tex && latex
PC.tex && latex PC.tex && dvips -o PC.ps PC.dvi
Be aware of the fact that sometimes LyX does ugly things!! Real ugly things.
It cut's of lines in my code.
For example
cout<<a<<endl<<b<<endl<<c<<endl<<d;
might be cut into two lines, and then all is lost.
WHY is LyX doing this to me??? I don't get it, perhaps some LyX guru knows.
Please respond with improvements.
Here is the C++ code that I use for the LyX2LaTeX2hightlight2LaTeX program
It's ugly, but it works. There are still a few bugs (undetected c++ code that
doesn't transforms into something nice), like
#include transforms into \#include, but I'll fix that soon.
====================================
/*Converts C++ (or other languages) code that is written in my special
cpp (or other language) environment in LyX, then exported to*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
// ending should NOT include a dot.
int main(int argc, char* argv[])
{
if (argc!=2) {
cout<<"Usage: "<<argv[0]<<" latex_fil.tex"<<endl<<endl;
return 0;
}
bool codemode=false; // found code?
string line;
string texline;
string tmp="/tmp/.Big_secret_I_love_emacs_and_I_hate_vim.cpp";
string tmptex=tmp+".tex";
string sed_command = "sed -i \'s/{}``/\"/g\' "+tmp+
"; cat "+tmp+" | tr -s \"\'\" \\\"
>/tmp/.no_not_really ; "+
" mv /tmp/.no_not_really "+tmp+
"; sed -i \'s/{}//g\' "+tmp+
"; sed -i \'s/\\\\{/{/g\' "+tmp+"; sed -i
\'s/\\\\}/}/g\' "+tmp+
"; sed -i \'s/\\\\_/ /g\' "+tmp+ "; sed -i \'s/~/ /g\'
"+tmp +
"; sed -i \'s/\\\\textasciitilde{}/~/g\' "+tmp+
"; sed -i \'s/\\\\textbackslash{}/\\\\/g\' "+tmp+
"; sed -i \'s/\\\\,{}//g\' "+tmp+
"; sed -i \'s/\\\\,//g\' "+tmp+
"; sed -i \'s/\\\\&/\\&/g\' "+tmp+
"; sed -i \'s/{\\[}/\\[/g\' "+tmp+
"; sed -i \'s/{]}/]/g\' "+tmp+
"; sed -i \'s/{\\*}/\\*/g\' "+tmp;
string command="highlight -A -L -t 4 -I -f -r -q -i "+tmp+" -o "+tmptex;
vector<string> source;
vector<string> latex_out; // the resulting latex file
string codefile;
ofstream codefout;
ifstream codefin;
ifstream fin(argv[1]);
if (!fin.is_open())
{
cout<<"Could not open the file "<<argv[1]<<endl<<endl;
return -1;
}
cout<<" Opening "<<argv[1]<<endl;
while (getline(fin,line))
{
if (line!="\\begin{KODcpp}" && line!= "\\end{KODcpp}")
{
if (!codemode)
latex_out.push_back(line);
else
{
// write to the file
if (line[line.size()-1]=='\\' &&
line[line.size()-2]=='\\')
line=line.substr(0,line.size()-2);
codefout<<line<<endl;
}
}
else
{
if (!codemode)
{ // open file and write
codefout.open(tmp.c_str());
if (!codefout.is_open())
{
cout<<"ERROR: couldn't open "<<tmp<<"
for writing"<<endl;
}
latex_out.push_back("\\begin{KODcpp}");
codemode=true;
}
else
{ // close file and run highlight and then insert it
codefout.close();
codemode=false;
system(sed_command.c_str()); // remove special
chars.
system(command.c_str());
cout<<sed_command<<endl;
codefin.open(tmptex.c_str());
if (!codefin.is_open())
{
cout<<"ERROR: couldn't open
"<<tmptex<<" for reading"<<endl;
}
while (getline(codefin,texline))
{
latex_out.push_back(texline);
}
codefin.close();
codefin.clear();
latex_out.push_back("\\end{KODcpp}");
}
}
}
fin.close();
ofstream fout(argv[1]);
if (!fout.is_open())
{
cout<<"Could not open the file "<<argv[1]<<endl<<endl;
return -1;
}
cout<<" Writing "<<argv[1]<<endl;
for (unsigned int i=0;i<latex_out.size();i++)
fout<<latex_out[i]<<endl;
fout.close();
return 0;
}