File: cadabra2latex.cc

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (47 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (3)
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
#include <fstream>
#include <iostream>
#include <regex>
#include "DataCell.hh"

int main(int argc, char **argv)
	{
	if(argc<3) {
		std::cerr << "Usage: cadabra2latex [--segment] [cadabra notebook] [latex file]\n\n";
		std::cerr << "Convert a Cadabra v2 notebook to a standalone LaTeX file (plus images).\n";
		return -1;
		}

	int i=1;
	bool for_embedding=false;
	if(argc==4) {
		if(std::string(argv[1])=="--segment") {
			for_embedding=true;
			}
		++i;
		}
	std::string cdb_file=argv[i], latex_file=argv[i+1];

	std::ifstream file(cdb_file);
	std::string content, line;
	while(std::getline(file, line))
		content+=line;

	cadabra::DTree doc;
	JSON_deserialise(content, doc);
	std::size_t dotpos = latex_file.rfind('.');
	std::string base = latex_file.substr(0, dotpos);
	std::string latex = export_as_LaTeX(doc, base, for_embedding);

	if(for_embedding) {
		// Ensure all sections are numbered if this will be embedded in a larger
		// document.
		latex=std::regex_replace(latex, std::regex(R"(\\section\*\{)"), "\\section\{");
		latex=std::regex_replace(latex, std::regex(R"(\\subsection\*\{)"), "\\subsection\{");
		latex=std::regex_replace(latex, std::regex(R"(\\LaTeX\{\})"), "LaTeX{}");				
		}

	std::ofstream latexfile(latex_file);
	latexfile << latex;

	return 0;
	}