Hello,

I created a patch that adds a Feynman diagram inset into lyx. The inset is
based on the inst "Diagram" in the package feyn (
http://www.ctan.org/tex-archive/fonts/feyn/ ) . The inset is meant to
replace the hack described in the 3rd section of the following document:
http://www.technion.ac.il/~ronen/latex/lyx_quantum.pdf
http://www.technion.ac.il/~ronen/latex/lyx_quantum.lyx

The patch is based on 2.0 Alpha 6, and based mostly on copied code from
XYMatrix inset.

I will appreciate comments regarding the patch. Is it too late to include it
in 2.0?

Thanks,
Ronen Abravanel
diff -rupN lyx-2.0.0alpha6/src/insets/InsetCode.h lyx-2.0.0alpha6_feynFinal/src/insets/InsetCode.h
--- lyx-2.0.0alpha6/src/insets/InsetCode.h	2010-09-15 16:33:43.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/insets/InsetCode.h	2010-09-18 12:00:16.000000000 -0400
@@ -223,6 +223,8 @@ enum InsetCode {
 	///
 	PREVIEW_CODE,
 	///
+	MATH_DIAGRAM_CODE, //RCHANGE
+	///
 	INSET_CODE_SIZE,
 };
 
diff -rupN lyx-2.0.0alpha6/src/insets/Inset.cpp lyx-2.0.0alpha6_feynFinal/src/insets/Inset.cpp
--- lyx-2.0.0alpha6/src/insets/Inset.cpp	2010-09-15 16:33:42.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/insets/Inset.cpp	2010-09-18 12:00:16.000000000 -0400
@@ -168,6 +168,7 @@ static void build_translator()
 	insetnames[MATH_XARROW_CODE] = InsetName("mathxarrow");
 	insetnames[MATH_XYARROW_CODE] = InsetName("mathxyarrow");
 	insetnames[MATH_XYMATRIX_CODE] = InsetName("mathxymatrix");
+	insetnames[MATH_DIAGRAM_CODE] = InsetName("mathdyagram");
 	insetnames[MATH_MACRO_CODE] = InsetName("mathmacro");
 
 	passed = true;
diff -rupN lyx-2.0.0alpha6/src/LaTeXFeatures.cpp lyx-2.0.0alpha6_feynFinal/src/LaTeXFeatures.cpp
--- lyx-2.0.0alpha6/src/LaTeXFeatures.cpp	2010-09-15 16:33:41.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/LaTeXFeatures.cpp	2010-09-18 12:00:16.000000000 -0400
@@ -746,6 +746,9 @@ string const LaTeXFeatures::getPackages(
 	if (mustProvide("xy"))
 		packages << "\\usepackage[all]{xy}\n";
 
+	if (mustProvide("feyn"))
+		packages << "\\usepackage{feyn}\n"; //Diagram
+
 	if (mustProvide("ulem"))
 		packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
 			    "\\usepackage{ulem}\n";
diff -rupN lyx-2.0.0alpha6/src/lyxmathed.cpp lyx-2.0.0alpha6_feynFinal/src/lyxmathed.cpp
--- lyx-2.0.0alpha6/src/lyxmathed.cpp	2010-09-16 04:26:35.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/lyxmathed.cpp	2010-09-18 12:00:16.000000000 -0400
@@ -52,6 +52,7 @@
 #include "mathed/InsetMathUnknown.cpp" 
 #include "mathed/InsetMathXArrow.cpp" 
 #include "mathed/InsetMathXYMatrix.cpp" 
+#include "mathed/InsetMathDiagram.cpp"
 #include "mathed/MathAtom.cpp" 
 #include "mathed/MathAutoCorrect.cpp" 
 #include "mathed/MathData.cpp" 
diff -rupN lyx-2.0.0alpha6/src/Makefile.am lyx-2.0.0alpha6_feynFinal/src/Makefile.am
--- lyx-2.0.0alpha6/src/Makefile.am	2010-09-15 16:33:42.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/Makefile.am	2010-09-18 12:00:16.000000000 -0400
@@ -409,6 +409,7 @@ SOURCEFILESMATHED = \
 	mathed/InsetMathUnknown.cpp \
 	mathed/InsetMathXArrow.cpp \
 	mathed/InsetMathXYMatrix.cpp \
+	mathed/InsetMathDiagram.cpp \
 	mathed/MathAtom.cpp \
 	mathed/MathAutoCorrect.cpp \
 	mathed/MathData.cpp \
@@ -474,6 +475,7 @@ HEADERFILESMATHED = \
 	mathed/InsetMathUnknown.h \
 	mathed/InsetMathXArrow.h \
 	mathed/InsetMathXYMatrix.h \
+	mathed/InsetMathDiagram.h \
 	mathed/MathAtom.h \
 	mathed/MathAutoCorrect.h \
 	mathed/MathData.h \
diff -rupN lyx-2.0.0alpha6/src/mathed/InsetMathDiagram.cpp lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathDiagram.cpp
--- lyx-2.0.0alpha6/src/mathed/InsetMathDiagram.cpp	1969-12-31 19:00:00.000000000 -0500
+++ lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathDiagram.cpp	2010-09-18 12:00:16.000000000 -0400
@@ -0,0 +1,143 @@
+/**
+ * \file InsetMathDiagram.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz, Ronen Abravanel
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "InsetMathDiagram.h"
+
+#include "LaTeXFeatures.h"
+#include "MathStream.h"
+
+#include <ostream>
+
+namespace lyx {
+
+
+InsetMathDiagram::InsetMathDiagram(Buffer * buf, Length const & s, char c,
+	bool e) : InsetMathGrid(buf, 1, 1), spacing_(s), spacing_code_(c),
+	equal_spacing_(e)
+{
+}
+
+
+Inset * InsetMathDiagram::clone() const
+{
+	return new InsetMathDiagram(*this);
+}
+
+
+int InsetMathDiagram::colsep() const
+{
+	return 10;
+}
+
+
+int InsetMathDiagram::rowsep() const
+{
+	return 10;
+}
+
+
+void InsetMathDiagram::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+	if (mi.base.style == LM_ST_DISPLAY)
+		mi.base.style = LM_ST_TEXT;
+	InsetMathGrid::metrics(mi, dim);
+}
+
+
+void InsetMathDiagram::write(WriteStream & os) const
+{
+	MathEnsurer ensurer(os);
+	os << "\\Diagram";
+	if (equal_spacing_) {
+		os << "@!";
+		switch (spacing_code_) {
+		case '0':
+		case 'R':
+		case 'C':
+			os << spacing_code_;
+		}
+	} else {
+		switch (spacing_code_) {
+		case 'R':
+		case 'C':
+		case 'M':
+		case 'W':
+		case 'H':
+		case 'L':
+			os << '@' << spacing_code_ << '='
+			   << from_ascii(spacing_.asLatexString());
+			break;
+		default:
+			if (!spacing_.empty())
+				os << "@=" << from_ascii(spacing_.asLatexString());
+		}
+	}
+	os << '{';
+	InsetMathGrid::write(os);
+	os << "}\n";
+}
+
+
+void InsetMathDiagram::infoize(odocstream & os) const
+{
+	os << "Diagram ";
+	if (equal_spacing_) {
+		switch (spacing_code_) {
+		case '0':
+		case 'R':
+		case 'C':
+			os << '!' << spacing_code_ << ' ';
+		}
+	} else {
+		switch (spacing_code_) {
+		case 'R':
+		case 'C':
+		case 'M':
+		case 'W':
+		case 'H':
+		case 'L':
+			os << spacing_code_ << ' '
+			   << from_ascii(spacing_.asLatexString()) << ' ';
+			break;
+		default:
+			if (!spacing_.empty())
+				os << from_ascii(spacing_.asLatexString()) << ' ';
+		}
+	}
+	InsetMathGrid::infoize(os);
+}
+
+
+void InsetMathDiagram::normalize(NormalStream & os) const
+{
+	os << "[diagram ";
+	InsetMathGrid::normalize(os);
+	os << ']';
+}
+
+
+void InsetMathDiagram::maple(MapleStream & os) const
+{
+	os << "diagram(";
+	InsetMathGrid::maple(os);
+	os << ')';
+}
+
+
+void InsetMathDiagram::validate(LaTeXFeatures & features) const
+{
+	features.require("feyn");
+	InsetMathGrid::validate(features);
+}
+
+
+} // namespace lyx
diff -rupN lyx-2.0.0alpha6/src/mathed/InsetMathDiagram.h lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathDiagram.h
--- lyx-2.0.0alpha6/src/mathed/InsetMathDiagram.h	1969-12-31 19:00:00.000000000 -0500
+++ lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathDiagram.h	2010-09-18 12:00:16.000000000 -0400
@@ -0,0 +1,65 @@
+// -*- C++ -*-
+/**
+ * \file InsetMathDiagram.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz, Ronen Abravanel
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef MATH_DIAGRAM_H
+#define MATH_DIAGRAM_H
+
+#include "Length.h"
+#include "InsetMathGrid.h"
+
+
+namespace lyx {
+
+
+class InsetMathDiagram : public InsetMathGrid {
+public:
+	///
+	InsetMathDiagram(Buffer * buf, Length const & = Length(), char c = '\0',
+		bool equal_spacing = false);
+	///
+	void metrics(MetricsInfo &, Dimension &) const;
+	///
+	InsetMathDiagram const * asDiagramInset() const { return this; }
+	///
+	virtual int colsep() const;
+	///
+	virtual int rowsep() const;
+
+	///
+	void normalize();
+	///
+	void write(WriteStream & os) const;
+	///
+	void infoize(odocstream & os) const;
+	///
+	void normalize(NormalStream &) const;
+	///
+	void maple(MapleStream &) const;
+	///
+	void validate(LaTeXFeatures & features) const;
+	///
+	InsetCode lyxCode() const { return MATH_DIAGRAM_CODE; }
+
+private:
+	///
+	virtual Inset * clone() const;
+	/// extra spacing, may be empty
+	Length spacing_;
+	///
+	char spacing_code_;
+	///
+	bool equal_spacing_;
+};
+
+
+
+} // namespace lyx
+#endif
diff -rupN lyx-2.0.0alpha6/src/mathed/InsetMathNest.cpp lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathNest.cpp
--- lyx-2.0.0alpha6/src/mathed/InsetMathNest.cpp	2010-09-15 16:33:43.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/mathed/InsetMathNest.cpp	2010-09-18 12:00:16.000000000 -0400
@@ -1994,6 +1994,7 @@ MathCompletionList::MathCompletionList(C
 	globals.push_back(from_ascii("\\cases"));
 	globals.push_back(from_ascii("\\substack"));
 	globals.push_back(from_ascii("\\xymatrix"));
+	globals.push_back(from_ascii("\\Diagram"));
 	globals.push_back(from_ascii("\\subarray"));
 	globals.push_back(from_ascii("\\array"));
 	globals.push_back(from_ascii("\\sqrt"));
diff -rupN lyx-2.0.0alpha6/src/mathed/MathFactory.cpp lyx-2.0.0alpha6_feynFinal/src/mathed/MathFactory.cpp
--- lyx-2.0.0alpha6/src/mathed/MathFactory.cpp	2010-09-15 16:33:43.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/mathed/MathFactory.cpp	2010-09-18 12:19:44.000000000 -0400
@@ -44,6 +44,7 @@
 #include "InsetMathHull.h"
 #include "InsetMathXArrow.h"
 #include "InsetMathXYMatrix.h"
+#include "InsetMathDiagram.h"
 #include "MacroTable.h"
 #include "MathMacro.h"
 #include "MathMacroArgument.h"
@@ -417,6 +418,48 @@ MathAtom createInsetMath(docstring const
 		return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
 			equal_spacing));
 	}
+
+	if (s.substr(0, 8) == "Diagram") {
+		char spacing_code = '\0';
+		Length spacing;
+		bool equal_spacing = false;
+		size_t const len = s.length();
+		size_t i = 8;
+		if (i < len && s[i] == '@') {
+			++i;
+			if (i < len && s[i] == '!') {
+				equal_spacing = true;
+				++i;
+				if (i < len) {
+					switch (s[i]) {
+					case '0':
+					case 'R':
+					case 'C':
+						spacing_code = static_cast<char>(s[i]);
+					}
+				}
+			} else if (i < len) {
+				switch (s[i]) {
+				case 'R':
+				case 'C':
+				case 'M':
+				case 'W':
+				case 'H':
+				case 'L':
+					spacing_code = static_cast<char>(s[i]);
+					++i;
+					break;
+				}
+				if (i < len && s[i] == '=') {
+					++i;
+					spacing = Length(to_ascii(s.substr(i)));
+				}
+			}
+		}
+		return MathAtom(new InsetMathDiagram(buf, spacing, spacing_code,
+			equal_spacing));
+	}
+
 	if (s == "xrightarrow" || s == "xleftarrow")
 		return MathAtom(new InsetMathXArrow(buf, s));
 	if (s == "split" || s == "alignedat")
diff -rupN lyx-2.0.0alpha6/src/mathed/MathParser.cpp lyx-2.0.0alpha6_feynFinal/src/mathed/MathParser.cpp
--- lyx-2.0.0alpha6/src/mathed/MathParser.cpp	2010-09-15 16:33:43.000000000 -0400
+++ lyx-2.0.0alpha6_feynFinal/src/mathed/MathParser.cpp	2010-09-18 12:20:22.000000000 -0400
@@ -1727,6 +1727,14 @@ bool Parser::parse1(InsetMathGrid & grid
 				delEmptyLastRow(subgrid);
 		}
 
+		else if (t.cs() == "diagram") {
+			odocstringstream os;
+			while (good() && nextToken().cat() != catBegin)
+				os << getToken().asInput();
+			cell->push_back(createInsetMath(t.cs() + os.str(), buf));
+			parse2(cell->back(), FLAG_ITEM, mode, false);
+		}
+
 		else if (t.cs() == "framebox" || t.cs() == "makebox") {
 			cell->push_back(createInsetMath(t.cs(), buf));
 			parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);

Reply via email to