3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "support/lassert.h"
22 #include "support/convert.h"
23 #include "support/lstrings.h"
34 // filled in preamble.cpp
35 map<char, int> special_columns;
42 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
45 /// vertical alignment
49 /// special column alignment
51 /// number of lines on the right
53 /// number of lines on the left
58 /// row type for longtables
65 /// part of head on first page
69 /// part of foot on last page
76 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
77 caption(false), newpage(false) {}
78 /// horizontal line above
80 /// horizontal line below
82 /// Extra space between the top line and this row
84 /// Extra space between this row and the bottom line
86 /// Extra space between the bottom line and the next top line
87 string interline_space;
88 /// These are for longtabulars only
89 /// row type (head, foot, firsthead etc.)
98 /// the numeric values are part of the file format!
102 /// A multicolumn cell. The number of columns is <tt>1 + number
103 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
104 CELL_BEGIN_OF_MULTICOLUMN = 1,
105 /// This is a dummy cell (part of a multicolumn cell)
106 CELL_PART_OF_MULTICOLUMN = 2
112 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
113 leftlines(0), rightlines(0), topline(false),
114 bottomline(false), rotate(false) {}
121 /// vertical cell alignment
123 /// number of lines on the left
125 /// number of lines on the right
127 /// do we have a line above?
129 /// do we have a line below?
131 /// is the cell rotated?
133 /// width for multicolumn cells
135 /// special formatting for multicolumn cells
140 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
141 inline char const * verbose_align(char c)
156 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
157 inline char const * verbose_valign(char c)
159 // The default value for no special alignment is "top".
172 // stripped down from tabluar.C. We use it currently only for bools and
174 string const write_attribute(string const & name, bool const & b)
176 // we write only true attribute values so we remove a bit of the
177 // file format bloat for tabulars.
178 return b ? ' ' + name + "=\"true\"" : string();
182 string const write_attribute(string const & name, string const & s)
184 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
188 /*! rather brutish way to code table structure in a string:
193 \multicolumn{2}{c}{4} & 5 //
199 gets "translated" to:
202 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
203 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
204 HLINE 6 TAB 7 HLINE HLINE LINE
205 HLINE 8 HLINE \endhead HLINE LINE
209 char const TAB = '\001';
210 char const LINE = '\002';
211 char const HLINE = '\004';
215 * Move the information in leftlines, rightlines, align and valign to the
216 * special field. This is necessary if the special field is not empty,
217 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
220 void ci2special(ColInfo & ci)
222 if (ci.width.empty() && ci.align == 'n')
223 // The alignment setting is already in special, since
224 // handle_colalign() never stores ci with these settings
225 // and ensures that leftlines == 0 and rightlines == 0 in
229 if (!ci.width.empty()) {
232 ci.special += ">{\\raggedright}";
235 ci.special += ">{\\raggedleft}";
238 ci.special += ">{\\centering}";
241 if (ci.valign == 'n')
244 ci.special += ci.valign;
245 ci.special += '{' + ci.width + '}';
248 ci.special += ci.align;
250 // LyX can only have one left and one right line.
251 for (int i = 1; i < ci.leftlines; ++i)
252 ci.special.insert(0, "|");
253 for (int i = 1; i < ci.rightlines; ++i)
255 ci.leftlines = min(ci.leftlines, 1);
256 ci.rightlines = min(ci.rightlines, 1);
263 * Handle column specifications for tabulars and multicolumns.
264 * The next token of the parser \p p must be an opening brace, and we read
265 * everything until the matching closing brace.
266 * The resulting column specifications are filled into \p colinfo. This is
267 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
269 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
270 ColInfo const & start)
272 if (p.get_token().cat() != catBegin)
273 cerr << "Wrong syntax for table column alignment.\n"
274 "Expected '{', got '" << p.curr_token().asInput()
277 ColInfo next = start;
278 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
281 cerr << "t: " << t << " c: '" << t.character() << "'\n";
284 // We cannot handle comments here
285 if (t.cat() == catComment) {
286 if (t.cs().empty()) {
290 cerr << "Ignoring comment: " << t.asInput();
294 switch (t.character()) {
298 // new column, horizontal aligned
299 next.align = t.character();
300 if (!next.special.empty())
302 colinfo.push_back(next);
308 // new column, vertical aligned box
309 next.valign = t.character();
310 next.width = p.verbatim_item();
311 if (!next.special.empty())
313 colinfo.push_back(next);
318 if (colinfo.empty()) {
319 if (next.special.empty())
323 } else if (colinfo.back().special.empty())
324 ++colinfo.back().rightlines;
325 else if (next.special.empty())
328 colinfo.back().special += '|';
331 // text before the next column
332 string const s = trimSpaceAndEol(p.verbatim_item());
333 if (next.special.empty() &&
335 // Maybe this can be converted to a
336 // horizontal alignment setting for
337 // fixed width columns
338 if (s == "\\raggedleft")
340 else if (s == "\\raggedright")
342 else if (s == "\\centering")
345 next.special = ">{" + s + '}';
347 next.special += ">{" + s + '}';
351 // text after the last column
352 string const s = trimSpaceAndEol(p.verbatim_item());
354 // This is not possible in LaTeX.
355 cerr << "Ignoring separator '<{"
356 << s << "}'." << endl;
358 ColInfo & ci = colinfo.back();
360 ci.special += "<{" + s + '}';
365 // *{n}{arg} means 'n' columns of type 'arg'
366 string const num = p.verbatim_item();
367 string const arg = p.verbatim_item();
368 size_t const n = convert<unsigned int>(num);
369 if (!arg.empty() && n > 0) {
371 for (size_t i = 0; i < n; ++i)
375 handle_colalign(p2, colinfo, next);
378 cerr << "Ignoring column specification"
379 " '*{" << num << "}{"
380 << arg << "}'." << endl;
385 // text instead of the column spacing
387 // text in addition to the column spacing
388 next.special += t.character();
389 next.special += '{' + p.verbatim_item() + '}';
392 // try user defined column types
393 if (special_columns.find(t.character()) !=
394 special_columns.end()) {
396 next.special += t.character();
398 special_columns[t.character()];
399 for (int i = 0; i < nargs; ++i)
400 next.special += '{' +
403 colinfo.push_back(next);
406 cerr << "Ignoring column specification"
407 " '" << t << "'." << endl;
412 // Maybe we have some column separators that need to be added to the
415 if (!next.special.empty()) {
416 ColInfo & ci = colinfo.back();
418 ci.special += next.special;
419 next.special.erase();
425 * Move the left and right lines and alignment settings of the column \p ci
426 * to the special field if necessary.
428 void fix_colalign(ColInfo & ci)
430 if (ci.leftlines > 1 || ci.rightlines > 1)
436 * LyX can't handle more than one vertical line at the left or right side
438 * This function moves the left and right lines and alignment settings of all
439 * columns in \p colinfo to the special field if necessary.
441 void fix_colalign(vector<ColInfo> & colinfo)
443 // Try to move extra leftlines to the previous column.
444 // We do this only if both special fields are empty, otherwise we
445 // can't tell wether the result will be the same.
446 for (size_t col = 0; col < colinfo.size(); ++col) {
447 if (colinfo[col].leftlines > 1 &&
448 colinfo[col].special.empty() && col > 0 &&
449 colinfo[col - 1].rightlines == 0 &&
450 colinfo[col - 1].special.empty()) {
451 ++colinfo[col - 1].rightlines;
452 --colinfo[col].leftlines;
455 // Try to move extra rightlines to the next column
456 for (size_t col = 0; col < colinfo.size(); ++col) {
457 if (colinfo[col].rightlines > 1 &&
458 colinfo[col].special.empty() &&
459 col < colinfo.size() - 1 &&
460 colinfo[col + 1].leftlines == 0 &&
461 colinfo[col + 1].special.empty()) {
462 ++colinfo[col + 1].leftlines;
463 --colinfo[col].rightlines;
466 // Move the lines and alignment settings to the special field if
468 for (size_t col = 0; col < colinfo.size(); ++col)
469 fix_colalign(colinfo[col]);
474 * Parse hlines and similar stuff.
475 * \returns wether the token \p t was parsed
477 bool parse_hlines(Parser & p, Token const & t, string & hlines,
478 bool is_long_tabular)
480 LASSERT(t.cat() == catEscape, return false);
482 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
483 t.cs() == "bottomrule")
484 hlines += '\\' + t.cs();
486 else if (t.cs() == "cline")
487 hlines += "\\cline{" + p.verbatim_item() + '}';
489 else if (t.cs() == "cmidrule") {
490 // We cannot handle the \cmidrule(l){3-4} form
493 bool const hasParentheses(p.getFullArg('(', ')').first);
497 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
500 else if (t.cs() == "addlinespace") {
503 bool const hasArgument(p.getFullArg('{', '}').first);
506 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
508 hlines += "\\addlinespace";
511 else if (is_long_tabular && t.cs() == "newpage")
512 hlines += "\\newpage";
521 /// Position in a row
523 /// At the very beginning, before the first token
525 /// After the first token and before any column token
527 /// After the first column token. Comments and whitespace are only
528 /// treated as tokens in this position
530 /// After the first non-column token at the end
536 * Parse table structure.
537 * We parse tables in a two-pass process: This function extracts the table
538 * structure (rows, columns, hlines etc.), but does not change the cell
539 * content. The cell content is parsed in a second step in handle_tabular().
541 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
542 RowPosition & pos, unsigned flags)
544 // table structure commands such as \hline
547 // comments that occur at places where we can't handle them
551 Token const & t = p.get_token();
554 debugToken(cerr, t, flags);
557 // comments and whitespace in hlines
560 case IN_HLINES_START:
562 if (t.cat() == catComment) {
567 // We can't handle comments here,
568 // store them for later use
569 comments += t.asInput();
571 } else if (t.cat() == catSpace ||
572 t.cat() == catNewline) {
573 // whitespace is irrelevant here, we
574 // need to recognize hline stuff
583 // We need to handle structure stuff first in order to
584 // determine wether we need to output a HLINE separator
585 // before the row or not.
586 if (t.cat() == catEscape) {
587 if (parse_hlines(p, t, hlines, is_long_tabular)) {
590 pos = IN_HLINES_START;
595 case IN_HLINES_START:
602 else if (t.cs() == "tabularnewline" ||
606 cerr << "Warning: Converting TeX "
607 "'\\cr' to LaTeX '\\\\'."
609 // stuff before the line break
610 os << comments << HLINE << hlines << HLINE
612 //cerr << "hlines: " << hlines << endl;
619 else if (is_long_tabular &&
620 (t.cs() == "endhead" ||
621 t.cs() == "endfirsthead" ||
622 t.cs() == "endfoot" ||
623 t.cs() == "endlastfoot")) {
624 hlines += t.asInput();
628 // these commands are implicit line
630 os << comments << HLINE << hlines
637 pos = IN_HLINES_START;
639 case IN_HLINES_START:
647 // We need a HLINE separator if we either have no hline
648 // stuff at all and are just starting a row or if we just
649 // got the first non-hline token.
652 // no hline tokens exist, first token at row start
653 case IN_HLINES_START:
654 // hline tokens exist, first non-hline token at row
656 os << hlines << HLINE << comments;
662 // Oops, there is still cell content or unsupported
663 // booktabs commands after hline stuff. The latter are
664 // moved to the cell, and the first does not work in
665 // LaTeX, so we ignore the hlines.
668 if (support::contains(hlines, "\\hline") ||
669 support::contains(hlines, "\\cline") ||
670 support::contains(hlines, "\\newpage"))
671 cerr << "Ignoring '" << hlines
672 << "' in a cell" << endl;
682 // If we come here we have normal cell content
686 if (t.cat() == catMath) {
687 // we are inside some text mode thingy, so opening new math is allowed
688 Token const & n = p.get_token();
689 if (n.cat() == catMath) {
690 // TeX's $$...$$ syntax for displayed math
692 // This does only work because parse_math outputs TeX
693 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
695 p.get_token(); // skip the second '$' token
697 // simple $...$ stuff
700 // This does only work because parse_math outputs TeX
701 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
706 else if (t.cat() == catSpace
707 || t.cat() == catNewline
708 || t.cat() == catLetter
709 || t.cat() == catSuper
711 || t.cat() == catOther
712 || t.cat() == catActive
713 || t.cat() == catParameter)
716 else if (t.cat() == catBegin) {
718 parse_table(p, os, is_long_tabular, pos,
723 else if (t.cat() == catEnd) {
724 if (flags & FLAG_BRACE_LAST)
726 cerr << "unexpected '}'\n";
729 else if (t.cat() == catAlign) {
734 else if (t.cat() == catComment)
737 else if (t.cs() == "(") {
739 // This does only work because parse_math outputs TeX
740 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
744 else if (t.cs() == "[") {
746 // This does only work because parse_math outputs TeX
747 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
751 else if (t.cs() == "begin") {
752 string const name = p.getArg('{', '}');
753 active_environments.push_back(name);
754 os << "\\begin{" << name << '}';
755 // treat the nested environment as a block, don't
756 // parse &, \\ etc, because they don't belong to our
757 // table if they appear.
758 os << p.verbatimEnvironment(name);
759 os << "\\end{" << name << '}';
760 active_environments.pop_back();
763 else if (t.cs() == "end") {
764 if (flags & FLAG_END) {
765 // eat environment name
766 string const name = p.getArg('{', '}');
767 if (name != active_environment())
768 p.error("\\end{" + name + "} does not match \\begin{"
769 + active_environment() + "}");
772 p.error("found 'end' unexpectedly");
779 // We can have comments if the last line is incomplete
782 // We can have hline stuff if the last line is incomplete
783 if (!hlines.empty()) {
784 // this does not work in LaTeX, so we ignore it
785 cerr << "Ignoring '" << hlines << "' at end of tabular"
791 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
794 for (size_t col = 0; col < ci.size(); ++col)
795 ci[col].topline = true;
799 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
801 ri.bottomline = true;
802 for (size_t col = 0; col < ci.size(); ++col)
803 ci[col].bottomline = true;
807 } // anonymous namespace
810 void handle_tabular(Parser & p, ostream & os, string const & name,
811 string const & tabularwidth, Context & context)
813 bool const is_long_tabular(name == "longtable");
814 bool booktabs = false;
815 string tabularvalignment("middle");
816 string posopts = p.getOpt();
817 if (!posopts.empty()) {
818 // FIXME: Convert this to ERT
820 cerr << "horizontal longtable positioning '"
821 << posopts << "' ignored\n";
822 else if (posopts == "[t]")
823 tabularvalignment = "top";
824 else if (posopts == "[b]")
825 tabularvalignment = "bottom";
827 cerr << "vertical tabular positioning '"
828 << posopts << "' ignored\n";
831 vector<ColInfo> colinfo;
833 // handle column formatting
834 handle_colalign(p, colinfo, ColInfo());
835 fix_colalign(colinfo);
837 // first scan of cells
838 // use table mode to keep it minimal-invasive
839 // not exactly what's TeX doing...
840 vector<string> lines;
842 RowPosition rowpos = ROW_START;
843 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
844 split(ss.str(), lines, LINE);
846 vector< vector<CellInfo> > cellinfo(lines.size());
847 vector<RowInfo> rowinfo(lines.size());
850 //cerr << "// split into rows\n";
851 for (size_t row = 0; row < rowinfo.size(); ++row) {
854 cellinfo[row].resize(colinfo.size());
857 vector<string> dummy;
858 //cerr << "\n########### LINE: " << lines[row] << "########\n";
859 split(lines[row], dummy, HLINE);
861 // handle horizontal line fragments
862 // we do only expect this for a last line without '\\'
863 if (dummy.size() != 3) {
864 if ((dummy.size() != 1 && dummy.size() != 2) ||
865 row != rowinfo.size() - 1)
866 cerr << "unexpected dummy size: " << dummy.size()
867 << " content: " << lines[row] << "\n";
870 lines[row] = dummy[1];
872 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
873 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
874 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
876 for (int i = 0; i <= 2; i += 2) {
877 //cerr << " reading from line string '" << dummy[i] << "'\n";
880 Token t = p1.get_token();
881 //cerr << "read token: " << t << "\n";
882 if (t.cs() == "hline" || t.cs() == "toprule" ||
883 t.cs() == "midrule" ||
884 t.cs() == "bottomrule") {
885 if (t.cs() != "hline")
888 if (rowinfo[row].topline) {
889 if (row > 0) // extra bottomline above
890 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
892 cerr << "dropping extra "
894 //cerr << "below row: " << row-1 << endl;
896 handle_hline_above(rowinfo[row], cellinfo[row]);
897 //cerr << "above row: " << row << endl;
900 //cerr << "below row: " << row << endl;
901 handle_hline_below(rowinfo[row], cellinfo[row]);
903 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
904 if (t.cs() == "cmidrule")
906 string arg = p1.verbatim_item();
907 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
909 split(arg, cols, '-');
911 size_t from = convert<unsigned int>(cols[0]);
913 cerr << "Could not parse "
914 << t.cs() << " start column."
917 // 1 based index -> 0 based
919 if (from >= colinfo.size()) {
920 cerr << t.cs() << " starts at "
921 "non existing column "
922 << (from + 1) << endl;
923 from = colinfo.size() - 1;
925 size_t to = convert<unsigned int>(cols[1]);
927 cerr << "Could not parse "
928 << t.cs() << " end column."
931 // 1 based index -> 0 based
933 if (to >= colinfo.size()) {
934 cerr << t.cs() << " ends at "
935 "non existing column "
937 to = colinfo.size() - 1;
939 for (size_t col = from; col <= to; ++col) {
940 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
942 rowinfo[row].topline = true;
943 cellinfo[row][col].topline = true;
945 rowinfo[row].bottomline = true;
946 cellinfo[row][col].bottomline = true;
949 } else if (t.cs() == "addlinespace") {
951 string const opt = p.next_token().cat() == catBegin ?
952 p.verbatim_item() : string();
955 rowinfo[row].top_space = "default";
957 rowinfo[row].top_space = translate_len(opt);
958 } else if (rowinfo[row].bottomline) {
960 rowinfo[row].bottom_space = "default";
962 rowinfo[row].bottom_space = translate_len(opt);
965 rowinfo[row].interline_space = "default";
967 rowinfo[row].interline_space = translate_len(opt);
969 } else if (t.cs() == "endhead") {
971 rowinfo[row].type = LT_HEAD;
972 for (int r = row - 1; r >= 0; --r) {
973 if (rowinfo[r].type != LT_NORMAL)
975 rowinfo[r].type = LT_HEAD;
977 } else if (t.cs() == "endfirsthead") {
979 rowinfo[row].type = LT_FIRSTHEAD;
980 for (int r = row - 1; r >= 0; --r) {
981 if (rowinfo[r].type != LT_NORMAL)
983 rowinfo[r].type = LT_FIRSTHEAD;
985 } else if (t.cs() == "endfoot") {
987 rowinfo[row].type = LT_FOOT;
988 for (int r = row - 1; r >= 0; --r) {
989 if (rowinfo[r].type != LT_NORMAL)
991 rowinfo[r].type = LT_FOOT;
993 } else if (t.cs() == "endlastfoot") {
995 rowinfo[row].type = LT_LASTFOOT;
996 for (int r = row - 1; r >= 0; --r) {
997 if (rowinfo[r].type != LT_NORMAL)
999 rowinfo[r].type = LT_LASTFOOT;
1001 } else if (t.cs() == "newpage") {
1004 rowinfo[row - 1].newpage = true;
1006 // This does not work in LaTeX
1012 rowinfo[row].newpage = true;
1014 cerr << "unexpected line token: " << t << endl;
1020 vector<string> cells;
1021 split(lines[row], cells, TAB);
1022 for (size_t col = 0, cell = 0; cell < cells.size();
1024 //cerr << "cell content: '" << cells[cell] << "'\n";
1025 if (col >= colinfo.size()) {
1026 // This does not work in LaTeX
1027 cerr << "Ignoring extra cell '"
1028 << cells[cell] << "'." << endl;
1031 Parser p(cells[cell]);
1033 //cells[cell] << "'\n";
1034 if (p.next_token().cs() == "multicolumn") {
1037 size_t const ncells =
1038 convert<unsigned int>(p.verbatim_item());
1040 // special cell properties alignment
1042 handle_colalign(p, t, ColInfo());
1043 p.skip_spaces(true);
1044 ColInfo & ci = t.front();
1046 // The logic of LyX for multicolumn vertical
1047 // lines is too complicated to reproduce it
1048 // here (see LyXTabular::TeXCellPreamble()).
1049 // Therefore we simply put everything in the
1053 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1054 cellinfo[row][col].align = ci.align;
1055 cellinfo[row][col].special = ci.special;
1056 cellinfo[row][col].leftlines = ci.leftlines;
1057 cellinfo[row][col].rightlines = ci.rightlines;
1059 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1060 if (!cellinfo[row][col].content.empty()) {
1061 // This may or may not work in LaTeX,
1062 // but it does not work in LyX.
1063 // FIXME: Handle it correctly!
1064 cerr << "Moving cell content '"
1066 << "' into a multicolumn cell. "
1067 "This will probably not work."
1070 cellinfo[row][col].content += os.str();
1072 // add dummy cells for multicol
1073 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
1075 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1076 cellinfo[row][col].align = 'c';
1079 } else if (col == 0 && is_long_tabular &&
1080 p.next_token().cs() == "caption") {
1081 // longtable caption support in LyX is a hack:
1082 // Captions require a row of their own with
1083 // the caption flag set to true, having only
1084 // one multicolumn cell. The contents of that
1085 // cell must contain exactly one caption inset
1086 // and nothing else.
1087 rowinfo[row].caption = true;
1088 for (size_t c = 1; c < cells.size(); ++c) {
1089 if (!cells[c].empty()) {
1090 cerr << "Moving cell content '"
1092 << "' into the caption cell. "
1093 "This will probably not work."
1095 cells[0] += cells[c];
1099 cellinfo[row][col].align = colinfo[col].align;
1100 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1102 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1103 cellinfo[row][col].content += os.str();
1104 // add dummy multicolumn cells
1105 for (size_t c = 1; c < colinfo.size(); ++c)
1106 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1109 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1110 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1111 cellinfo[row][col].align = colinfo[col].align;
1113 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1114 cellinfo[row][col].content += os.str();
1118 //cerr << "// handle almost empty last row what we have\n";
1119 // handle almost empty last row
1120 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1121 //cerr << "remove empty last line\n";
1122 if (rowinfo[row].topline)
1123 rowinfo[row - 1].bottomline = true;
1124 for (size_t col = 0; col < colinfo.size(); ++col)
1125 if (cellinfo[row][col].topline)
1126 cellinfo[row - 1][col].bottomline = true;
1131 // Now we have the table structure and content in rowinfo, colinfo
1133 // Unfortunately LyX has some limitations that we need to work around.
1135 // Convert cells with special content to multicolumn cells
1136 // (LyX ignores the special field for non-multicolumn cells).
1137 for (size_t row = 0; row < rowinfo.size(); ++row) {
1138 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1139 if (cellinfo[row][col].multi == CELL_NORMAL &&
1140 !cellinfo[row][col].special.empty())
1141 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1145 // Distribute lines from rows/columns to cells
1146 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1147 // Each standard cell inherits the settings of the corresponding
1148 // rowinfo/colinfo. This works because all cells with individual
1149 // settings were converted to multicolumn cells above.
1150 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1151 // corresponding to the first column of the multicolumn cell (default
1152 // of the multicol package). This works because the special field
1153 // overrides the line fields.
1154 for (size_t row = 0; row < rowinfo.size(); ++row) {
1155 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1156 if (cellinfo[row][col].multi == CELL_NORMAL) {
1157 cellinfo[row][col].topline = rowinfo[row].topline;
1158 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1159 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1160 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1161 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1163 while (s < cellinfo[row].size() &&
1164 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1166 if (s < cellinfo[row].size() &&
1167 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1168 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1169 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1170 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1176 preamble.registerAutomaticallyLoadedPackage("booktabs");
1177 if (is_long_tabular)
1178 preamble.registerAutomaticallyLoadedPackage("longtable");
1180 //cerr << "// output what we have\n";
1181 // output what we have
1182 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1183 << "\" columns=\"" << colinfo.size() << "\">\n";
1185 << write_attribute("rotate", false)
1186 << write_attribute("booktabs", booktabs)
1187 << write_attribute("islongtable", is_long_tabular);
1188 if (!is_long_tabular)
1189 os << write_attribute("tabularvalignment", tabularvalignment)
1190 << write_attribute("tabularwidth", tabularwidth);
1193 //cerr << "// after header\n";
1194 for (size_t col = 0; col < colinfo.size(); ++col) {
1195 os << "<column alignment=\""
1196 << verbose_align(colinfo[col].align) << "\""
1198 << verbose_valign(colinfo[col].valign) << "\""
1199 << write_attribute("width", translate_len(colinfo[col].width))
1200 << write_attribute("special", colinfo[col].special)
1203 //cerr << "// after cols\n";
1205 for (size_t row = 0; row < rowinfo.size(); ++row) {
1207 << write_attribute("topspace", rowinfo[row].top_space)
1208 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1209 << write_attribute("interlinespace", rowinfo[row].interline_space)
1210 << write_attribute("endhead",
1211 rowinfo[row].type == LT_HEAD)
1212 << write_attribute("endfirsthead",
1213 rowinfo[row].type == LT_FIRSTHEAD)
1214 << write_attribute("endfoot",
1215 rowinfo[row].type == LT_FOOT)
1216 << write_attribute("endlastfoot",
1217 rowinfo[row].type == LT_LASTFOOT)
1218 << write_attribute("newpage", rowinfo[row].newpage)
1219 << write_attribute("caption", rowinfo[row].caption)
1221 for (size_t col = 0; col < colinfo.size(); ++col) {
1222 CellInfo const & cell = cellinfo[row][col];
1224 if (cell.multi != CELL_NORMAL)
1225 os << " multicolumn=\"" << cell.multi << "\"";
1226 os << " alignment=\"" << verbose_align(cell.align)
1228 << " valignment=\"" << verbose_valign(cell.valign)
1230 << write_attribute("topline", cell.topline)
1231 << write_attribute("bottomline", cell.bottomline)
1232 << write_attribute("leftline", cell.leftlines > 0)
1233 << write_attribute("rightline", cell.rightlines > 0)
1234 << write_attribute("rotate", cell.rotate);
1235 //cerr << "\nrow: " << row << " col: " << col;
1237 // cerr << " topline=\"true\"";
1238 //if (cell.bottomline)
1239 // cerr << " bottomline=\"true\"";
1240 os << " usebox=\"none\""
1241 << write_attribute("width", translate_len(cell.width));
1242 if (cell.multi != CELL_NORMAL)
1243 os << write_attribute("special", cell.special);
1245 << "\n\\begin_inset Text\n"
1247 << "\n\\end_inset\n"
1253 os << "</lyxtabular>\n";