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.
22 #include "support/lassert.h"
23 #include "support/convert.h"
24 #include "support/lstrings.h"
41 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
42 varwidth(false), decimal_point('\0') {}
45 /// vertical alignment
49 /// special column alignment
51 /// number of lines on the right
53 /// number of lines on the left
62 /// row type for longtables
69 /// part of head on first page
73 /// part of foot on last page
80 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
81 caption(false), newpage(false) {}
82 /// Does this row have any special setting?
85 return topline || bottomline || !top_space.empty() ||
86 !bottom_space.empty() || !interline_space.empty() ||
87 type != LT_NORMAL || caption || newpage;
89 /// horizontal line above
91 /// horizontal line below
93 /// Extra space between the top line and this row
95 /// Extra space between this row and the bottom line
97 /// Extra space between the bottom line and the next top line
98 string interline_space;
99 /// These are for longtabulars only
100 /// row type (head, foot, firsthead etc.)
102 /// row for a caption
104 /// row for a newpage
109 /// the numeric values are part of the file format!
113 /// A multicolumn cell. The number of columns is <tt>1 + number
114 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
115 CELL_BEGIN_OF_MULTICOLUMN,
116 /// This is a dummy cell (part of a multicolumn cell)
117 CELL_PART_OF_MULTICOLUMN,
119 CELL_BEGIN_OF_MULTIROW,
121 CELL_PART_OF_MULTIROW
127 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
128 leftlines(0), rightlines(0), topline(false),
129 bottomline(false), rotate(0), mrxnum(0) {}
136 /// vertical cell alignment
138 /// number of lines on the left
140 /// number of lines on the right
142 /// do we have a line above?
144 /// do we have a line below?
146 /// how is the cell rotated?
148 /// width for multicolumn cells
150 /// special formatting for multicolumn cells
154 /// number of further multirows
162 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
163 // we have this header type (is set in the getLT... functions)
165 // double borders on top
167 // double borders on bottom
169 // used for FirstHeader & LastFooter and if this is true
170 // all the rows marked as FirstHeader or LastFooter are
171 // ignored in the output and it is set to be empty!
176 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
177 inline char const * verbose_align(char c)
194 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
195 inline char const * verbose_valign(char c)
197 // The default value for no special alignment is "top".
210 // stripped down from tabluar.C. We use it currently only for bools and
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? ' ' + name + "=\"true\"" : string();
220 string const write_attribute(string const & name, string const & s)
222 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
226 string const write_attribute(string const & name, int const & i)
228 // we write only true attribute values so we remove a bit of the
229 // file format bloat for tabulars.
230 return i ? write_attribute(name, convert<string>(i)) : string();
234 /*! rather brutish way to code table structure in a string:
239 \multicolumn{2}{c}{4} & 5 //
245 gets "translated" to:
248 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
249 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
250 HLINE 6 TAB 7 HLINE HLINE LINE
251 HLINE 8 HLINE \endhead HLINE LINE
255 char const TAB = '\001';
256 char const LINE = '\002';
257 char const HLINE = '\004';
261 * Move the information in leftlines, rightlines, align and valign to the
262 * special field. This is necessary if the special field is not empty,
263 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
266 void ci2special(ColInfo & ci)
268 if (ci.width.empty() && ci.align == 'n')
269 // The alignment setting is already in special, since
270 // handle_colalign() never stores ci with these settings
271 // and ensures that leftlines == 0 and rightlines == 0 in
275 if (ci.decimal_point != '\0') {
276 // we only support decimal point natively
277 // with 'l' alignment in or 'n' alignment
278 // with width in second row
279 if (ci.align != 'l' && ci.align != 'n') {
280 ci.decimal_point = '\0';
286 if (!ci.width.empty()) {
287 string arraybackslash;
289 arraybackslash = "\\arraybackslash";
292 ci.special += ">{\\raggedright" + arraybackslash + "}";
295 ci.special += ">{\\raggedleft" + arraybackslash + "}";
298 ci.special += ">{\\centering" + arraybackslash + "}";
303 else if (ci.valign == 'n')
306 ci.special += ci.valign;
307 ci.special += '{' + ci.width + '}';
310 ci.special += ci.align;
312 // LyX can only have one left and one right line.
313 for (int i = 1; i < ci.leftlines; ++i)
314 ci.special.insert(0, "|");
315 for (int i = 1; i < ci.rightlines; ++i)
317 ci.leftlines = min(ci.leftlines, 1);
318 ci.rightlines = min(ci.rightlines, 1);
325 * Handle column specifications for tabulars and multicolumns.
326 * The next token of the parser \p p must be an opening brace, and we read
327 * everything until the matching closing brace.
328 * The resulting column specifications are filled into \p colinfo. This is
329 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
331 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
332 ColInfo const & start)
334 if (p.get_token().cat() != catBegin)
335 cerr << "Wrong syntax for table column alignment.\n"
336 "Expected '{', got '" << p.curr_token().asInput()
339 ColInfo next = start;
340 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
343 cerr << "t: " << t << " c: '" << t.character() << "'\n";
346 // We cannot handle comments here
347 if (t.cat() == catComment) {
348 if (t.cs().empty()) {
352 cerr << "Ignoring comment: " << t.asInput();
356 switch (t.character()) {
358 // whitespace, ignore.
363 // new column, horizontal aligned
364 next.align = t.character();
365 if (!next.special.empty()) {
367 // handle decimal separator
368 if (next.decimal_point != '\0') {
369 if (!colinfo.empty() && colinfo.back().align == 'r') {
370 colinfo.back().align = 'd';
371 colinfo.back().decimal_point = next.decimal_point;
373 next.decimal_point = '\0';
376 colinfo.push_back(next);
381 next.varwidth = true;
382 if (!next.special.empty())
384 colinfo.push_back(next);
390 // new column, vertical aligned box
391 next.valign = t.character();
392 next.width = p.verbatim_item();
393 if (!next.special.empty()) {
395 // handle decimal separator
396 if (next.decimal_point != '\0') {
397 if (!colinfo.empty() && colinfo.back().align == 'r') {
398 colinfo.back().align = 'd';
399 colinfo.back().decimal_point = next.decimal_point;
401 next.decimal_point = '\0';
404 colinfo.push_back(next);
409 if (colinfo.empty()) {
410 if (next.special.empty())
414 } else if (colinfo.back().special.empty())
415 ++colinfo.back().rightlines;
416 else if (next.special.empty())
419 colinfo.back().special += '|';
422 // text before the next column
423 string const s = trimSpaceAndEol(p.verbatim_item());
424 if (next.special.empty() &&
426 // Maybe this can be converted to a
427 // horizontal alignment setting for
428 // fixed width columns
429 if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash")
431 else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash")
433 else if (s == "\\centering" || s == "\\centering\\arraybackslash")
436 next.special = ">{" + s + '}';
438 next.special += ">{" + s + '}';
442 // text after the last column
443 string const s = trimSpaceAndEol(p.verbatim_item());
445 // This is not possible in LaTeX.
446 cerr << "Ignoring separator '<{"
447 << s << "}'." << endl;
449 ColInfo & ci = colinfo.back();
451 ci.special += "<{" + s + '}';
456 if (p.next_token().character() != '{')
458 // *{n}{arg} means 'n' columns of type 'arg'
459 string const num = p.verbatim_item();
460 string const arg = p.verbatim_item();
461 size_t const n = convert<unsigned int>(num);
462 if (!arg.empty() && n > 0) {
464 for (size_t i = 0; i < n; ++i)
468 handle_colalign(p2, colinfo, next);
471 cerr << "Ignoring column specification"
472 " '*{" << num << "}{"
473 << arg << "}'." << endl;
478 // text instead of the column spacing
480 // text in addition to the column spacing
481 string const arg = p.verbatim_item();
482 next.special += t.character();
483 next.special += '{' + arg + '}';
484 string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
485 if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
486 next.decimal_point = arg.back();
490 // try user defined column types
491 // unknown column types (nargs == -1) are
492 // assumed to consume no arguments
494 next.special += t.character();
496 preamble.getSpecialTableColumnArguments(t.character());
497 for (int i = 0; i < nargs; ++i)
498 next.special += '{' +
499 p.verbatim_item() + '}';
500 colinfo.push_back(next);
507 // Maybe we have some column separators that need to be added to the
510 if (!next.special.empty()) {
511 ColInfo & ci = colinfo.back();
513 ci.special += next.special;
514 next.special.erase();
520 * Move the left and right lines and alignment settings of the column \p ci
521 * to the special field if necessary.
523 void fix_colalign(ColInfo & ci)
525 if (ci.leftlines > 1 || ci.rightlines > 1)
531 * LyX can't handle more than one vertical line at the left or right side
533 * This function moves the left and right lines and alignment settings of all
534 * columns in \p colinfo to the special field if necessary.
536 void fix_colalign(vector<ColInfo> & colinfo)
538 // Try to move extra leftlines to the previous column.
539 // We do this only if both special fields are empty, otherwise we
540 // can't tell wether the result will be the same.
541 for (size_t col = 0; col < colinfo.size(); ++col) {
542 if (colinfo[col].leftlines > 1 &&
543 colinfo[col].special.empty() && col > 0 &&
544 colinfo[col - 1].rightlines == 0 &&
545 colinfo[col - 1].special.empty()) {
546 ++colinfo[col - 1].rightlines;
547 --colinfo[col].leftlines;
550 // Try to move extra rightlines to the next column
551 for (size_t col = 0; col < colinfo.size(); ++col) {
552 if (colinfo[col].rightlines > 1 &&
553 colinfo[col].special.empty() &&
554 col < colinfo.size() - 1 &&
555 colinfo[col + 1].leftlines == 0 &&
556 colinfo[col + 1].special.empty()) {
557 ++colinfo[col + 1].leftlines;
558 --colinfo[col].rightlines;
561 // Move the lines and alignment settings to the special field if
563 for (size_t col = 0; col < colinfo.size(); ++col)
564 fix_colalign(colinfo[col]);
569 * Parse hlines and similar stuff.
570 * \returns wether the token \p t was parsed
572 bool parse_hlines(Parser & p, Token const & t, string & hlines,
573 bool is_long_tabular)
575 LASSERT(t.cat() == catEscape, return false);
577 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
578 t.cs() == "bottomrule")
579 hlines += '\\' + t.cs();
581 else if (t.cs() == "cline")
582 hlines += "\\cline{" + p.verbatim_item() + '}';
584 else if (t.cs() == "cmidrule") {
585 // We cannot handle the \cmidrule(l){3-4} form
588 bool const hasParentheses(p.getFullArg('(', ')').first);
592 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
595 else if (t.cs() == "addlinespace") {
598 bool const hasArgument(p.getFullArg('{', '}').first);
601 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
603 hlines += "\\addlinespace";
606 else if (is_long_tabular && t.cs() == "newpage")
607 hlines += "\\newpage";
616 /// Position in a row
618 /// At the very beginning, before the first token
620 /// After the first token and before any column token
622 /// After the first column token. Comments and whitespace are only
623 /// treated as tokens in this position
625 /// After the first non-column token at the end
631 * Parse table structure.
632 * We parse tables in a two-pass process: This function extracts the table
633 * structure (rows, columns, hlines etc.), but does not change the cell
634 * content. The cell content is parsed in a second step in handle_tabular().
636 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
637 RowPosition & pos, unsigned flags)
639 // table structure commands such as \hline
642 // comments that occur at places where we can't handle them
646 Token const & t = p.get_token();
649 debugToken(cerr, t, flags);
652 // comments and whitespace in hlines
655 case IN_HLINES_START:
657 if (t.cat() == catComment) {
662 // We can't handle comments here,
663 // store them for later use
664 comments += t.asInput();
666 } else if (t.cat() == catSpace ||
667 t.cat() == catNewline) {
668 // whitespace is irrelevant here, we
669 // need to recognize hline stuff
678 // We need to handle structure stuff first in order to
679 // determine wether we need to output a HLINE separator
680 // before the row or not.
681 if (t.cat() == catEscape) {
682 if (parse_hlines(p, t, hlines, is_long_tabular)) {
685 pos = IN_HLINES_START;
690 case IN_HLINES_START:
697 else if (t.cs() == "tabularnewline" ||
701 cerr << "Warning: Converting TeX "
702 "'\\cr' to LaTeX '\\\\'."
704 // stuff before the line break
705 os << comments << HLINE << hlines << HLINE
707 //cerr << "hlines: " << hlines << endl;
714 else if (is_long_tabular &&
715 (t.cs() == "endhead" ||
716 t.cs() == "endfirsthead" ||
717 t.cs() == "endfoot" ||
718 t.cs() == "endlastfoot")) {
719 hlines += t.asInput();
723 // these commands are implicit line
725 os << comments << HLINE << hlines
732 pos = IN_HLINES_START;
734 case IN_HLINES_START:
741 // We need a HLINE separator if we either have no hline
742 // stuff at all and are just starting a row or if we just
743 // got the first non-hline token.
746 // no hline tokens exist, first token at row start
747 case IN_HLINES_START:
748 // hline tokens exist, first non-hline token at row
750 os << hlines << HLINE << comments;
756 // Oops, there is still cell content or unsupported
757 // booktabs commands after hline stuff. The latter are
758 // moved to the cell, and the first does not work in
759 // LaTeX, so we ignore the hlines.
762 if (support::contains(hlines, "\\hline") ||
763 support::contains(hlines, "\\cline") ||
764 support::contains(hlines, "\\newpage"))
765 cerr << "Ignoring '" << hlines
766 << "' in a cell" << endl;
776 // If we come here we have normal cell content
780 if (t.cat() == catMath) {
781 // we are inside some text mode thingy, so opening new math is allowed
782 Token const & n = p.get_token();
783 if (n.cat() == catMath) {
784 // TeX's $$...$$ syntax for displayed math
786 // This does only work because parse_math outputs TeX
787 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
789 p.get_token(); // skip the second '$' token
791 // simple $...$ stuff
794 // This does only work because parse_math outputs TeX
795 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
800 else if (t.cat() == catSpace
801 || t.cat() == catNewline
802 || t.cat() == catLetter
803 || t.cat() == catSuper
805 || t.cat() == catOther
806 || t.cat() == catActive
807 || t.cat() == catParameter)
810 else if (t.cat() == catBegin) {
812 parse_table(p, os, is_long_tabular, pos,
817 else if (t.cat() == catEnd) {
818 if (flags & FLAG_BRACE_LAST)
820 cerr << "unexpected '}'\n";
823 else if (t.cat() == catAlign) {
828 else if (t.cat() == catComment)
831 else if (t.cs() == "(") {
833 // This does only work because parse_math outputs TeX
834 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
838 else if (t.cs() == "[") {
840 // This does only work because parse_math outputs TeX
841 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
845 else if (t.cs() == "begin") {
846 string const name = p.getArg('{', '}');
847 active_environments.push_back(name);
848 os << "\\begin{" << name << '}';
849 // treat the nested environment as a block, don't
850 // parse &, \\ etc, because they don't belong to our
851 // table if they appear.
852 os << p.ertEnvironment(name);
853 os << "\\end{" << name << '}';
854 active_environments.pop_back();
857 else if (t.cs() == "end") {
858 if (flags & FLAG_END) {
859 // eat environment name
860 string const name = p.getArg('{', '}');
861 if (name != active_environment())
862 p.error("\\end{" + name + "} does not match \\begin{"
863 + active_environment() + "}");
866 p.error("found 'end' unexpectedly");
873 // We can have comments if the last line is incomplete
876 // We can have hline stuff if the last line is incomplete
877 if (!hlines.empty()) {
878 // this does not work in LaTeX, so we ignore it
879 cerr << "Ignoring '" << hlines << "' at end of tabular"
885 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
888 for (size_t col = 0; col < ci.size(); ++col)
889 ci[col].topline = true;
893 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
895 ri.bottomline = true;
896 for (size_t col = 0; col < ci.size(); ++col)
897 ci[col].bottomline = true;
901 } // anonymous namespace
904 void handle_tabular(Parser & p, ostream & os, string const & name,
905 string const & tabularwidth, string const & halign,
908 bool const is_long_tabular(name == "longtable" || name == "xltabular");
909 bool booktabs = false;
910 string tabularvalignment("middle");
911 string posopts = p.getOpt();
912 if (!posopts.empty()) {
913 if (posopts == "[t]")
914 tabularvalignment = "top";
915 else if (posopts == "[b]")
916 tabularvalignment = "bottom";
918 cerr << "vertical tabular positioning '"
919 << posopts << "' ignored\n";
922 vector<ColInfo> colinfo;
924 // handle column formatting
925 handle_colalign(p, colinfo, ColInfo());
926 fix_colalign(colinfo);
928 // first scan of cells
929 // use table mode to keep it minimal-invasive
930 // not exactly what's TeX doing...
931 vector<string> lines;
933 RowPosition rowpos = ROW_START;
934 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
935 split(ss.str(), lines, LINE);
937 vector< vector<CellInfo> > cellinfo(lines.size());
938 vector<RowInfo> rowinfo(lines.size());
945 //cerr << "// split into rows\n";
946 for (size_t row = 0; row < rowinfo.size();) {
949 cellinfo[row].resize(colinfo.size());
950 bool deletelastrow = false;
953 vector<string> dummy;
954 //cerr << "\n########### LINE: " << lines[row] << "########\n";
955 split(lines[row], dummy, HLINE);
957 // handle horizontal line fragments
958 // we do only expect this for a last line without '\\'
959 if (dummy.size() != 3) {
960 if ((dummy.size() != 1 && dummy.size() != 2) ||
961 row != rowinfo.size() - 1)
962 cerr << "unexpected dummy size: " << dummy.size()
963 << " content: " << lines[row] << "\n";
966 lines[row] = dummy[1];
968 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
969 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
970 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
972 for (int i = 0; i <= 2; i += 2) {
973 //cerr << " reading from line string '" << dummy[i] << "'\n";
976 Token t = p1.get_token();
977 //cerr << "read token: " << t << "\n";
978 if (t.cs() == "hline" || t.cs() == "toprule" ||
979 t.cs() == "midrule" ||
980 t.cs() == "bottomrule") {
981 if (t.cs() != "hline")
984 if (rowinfo[row].topline) {
985 if (row > 0) // extra bottomline above
986 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
988 cerr << "dropping extra "
990 //cerr << "below row: " << row-1 << endl;
992 handle_hline_above(rowinfo[row], cellinfo[row]);
993 //cerr << "above row: " << row << endl;
996 //cerr << "below row: " << row << endl;
997 handle_hline_below(rowinfo[row], cellinfo[row]);
999 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
1000 if (t.cs() == "cmidrule")
1002 string arg = p1.verbatim_item();
1003 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
1004 vector<string> cols;
1005 split(arg, cols, '-');
1007 size_t from = convert<unsigned int>(cols[0]);
1009 cerr << "Could not parse "
1010 << t.cs() << " start column."
1013 // 1 based index -> 0 based
1015 if (from >= colinfo.size()) {
1016 cerr << t.cs() << " starts at "
1017 "non existing column "
1018 << (from + 1) << endl;
1019 from = colinfo.size() - 1;
1021 size_t to = convert<unsigned int>(cols[1]);
1023 cerr << "Could not parse "
1024 << t.cs() << " end column."
1027 // 1 based index -> 0 based
1029 if (to >= colinfo.size()) {
1030 cerr << t.cs() << " ends at "
1031 "non existing column "
1032 << (to + 1) << endl;
1033 to = colinfo.size() - 1;
1035 for (size_t col = from; col <= to; ++col) {
1036 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
1038 rowinfo[row].topline = true;
1039 cellinfo[row][col].topline = true;
1041 rowinfo[row].bottomline = true;
1042 cellinfo[row][col].bottomline = true;
1045 } else if (t.cs() == "addlinespace") {
1047 string const opt = p.next_token().cat() == catBegin ?
1048 p.verbatim_item() : string();
1051 rowinfo[row].top_space = "default";
1053 rowinfo[row].top_space = translate_len(opt);
1054 } else if (rowinfo[row].bottomline) {
1056 rowinfo[row].bottom_space = "default";
1058 rowinfo[row].bottom_space = translate_len(opt);
1061 rowinfo[row].interline_space = "default";
1063 rowinfo[row].interline_space = translate_len(opt);
1065 } else if (t.cs() == "endhead") {
1067 endhead.empty = true;
1069 rowinfo[row].type = LT_HEAD;
1070 for (int r = row - 1; r >= 0; --r) {
1071 if (rowinfo[r].type != LT_NORMAL)
1073 rowinfo[r].type = LT_HEAD;
1074 endhead.empty = false;
1077 } else if (t.cs() == "endfirsthead") {
1079 endfirsthead.empty = true;
1081 rowinfo[row].type = LT_FIRSTHEAD;
1082 for (int r = row - 1; r >= 0; --r) {
1083 if (rowinfo[r].type != LT_NORMAL)
1085 rowinfo[r].type = LT_FIRSTHEAD;
1086 endfirsthead.empty = false;
1088 endfirsthead.set = true;
1089 } else if (t.cs() == "endfoot") {
1091 endfoot.empty = true;
1093 rowinfo[row].type = LT_FOOT;
1094 for (int r = row - 1; r >= 0; --r) {
1095 if (rowinfo[r].type != LT_NORMAL)
1097 rowinfo[r].type = LT_FOOT;
1098 endfoot.empty = false;
1101 } else if (t.cs() == "endlastfoot") {
1103 endlastfoot.empty = true;
1105 rowinfo[row].type = LT_LASTFOOT;
1106 for (int r = row - 1; r >= 0; --r) {
1107 if (rowinfo[r].type != LT_NORMAL)
1109 rowinfo[r].type = LT_LASTFOOT;
1110 endlastfoot.empty = false;
1112 endlastfoot.set = true;
1113 } else if (t.cs() == "newpage") {
1116 rowinfo[row - 1].newpage = true;
1118 // This does not work in LaTeX
1124 rowinfo[row].newpage = true;
1126 cerr << "unexpected line token: " << t << endl;
1131 // LyX ends headers and footers always with \tabularnewline.
1132 // This causes one additional row in the output.
1133 // If the last row of a header/footer is empty, we can work
1134 // around that by removing it.
1136 RowInfo test = rowinfo[row-1];
1137 test.type = LT_NORMAL;
1138 if (lines[row-1].empty() && !test.special()) {
1139 switch (rowinfo[row-1].type) {
1141 if (rowinfo[row].type != LT_FIRSTHEAD &&
1142 rowinfo[row-2].type == LT_FIRSTHEAD)
1143 deletelastrow = true;
1146 if (rowinfo[row].type != LT_HEAD &&
1147 rowinfo[row-2].type == LT_HEAD)
1148 deletelastrow = true;
1151 if (rowinfo[row].type != LT_FOOT &&
1152 rowinfo[row-2].type == LT_FOOT)
1153 deletelastrow = true;
1156 if (rowinfo[row].type != LT_LASTFOOT &&
1157 rowinfo[row-2].type == LT_LASTFOOT)
1158 deletelastrow = true;
1166 if (deletelastrow) {
1167 lines.erase(lines.begin() + (row - 1));
1168 rowinfo.erase(rowinfo.begin() + (row - 1));
1169 cellinfo.erase(cellinfo.begin() + (row - 1));
1174 vector<string> cells;
1175 split(lines[row], cells, TAB);
1176 for (size_t col = 0, cell = 0; cell < cells.size();
1178 //cerr << "cell content: '" << cells[cell] << "'\n";
1179 if (col >= colinfo.size()) {
1180 // This does not work in LaTeX
1181 cerr << "Ignoring extra cell '"
1182 << cells[cell] << "'." << endl;
1185 string cellcont = cells[cell];
1186 // For decimal cells, ass the content of the second one to the first one
1188 if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1)
1189 cellcont += colinfo[col].decimal_point + cells[cell + 1];
1190 Parser parse(cellcont);
1191 parse.skip_spaces();
1192 //cells[cell] << "'\n";
1193 if (parse.next_token().cs() == "multirow") {
1194 // We do not support the vpos arg yet.
1195 if (parse.hasOpt()) {
1196 string const vpos = parse.getArg('[', ']');
1197 parse.skip_spaces(true);
1198 cerr << "Ignoring multirow's vpos arg '"
1199 << vpos << "'!" << endl;
1203 size_t const ncells =
1204 convert<unsigned int>(parse.verbatim_item());
1205 // We do not support the bigstrut arg yet.
1206 if (parse.hasOpt()) {
1207 string const bs = parse.getArg('[', ']');
1208 parse.skip_spaces(true);
1209 cerr << "Ignoring multirow's bigstrut arg '"
1210 << bs << "'!" << endl;
1212 // the width argument
1213 string const width = parse.getArg('{', '}');
1216 if (parse.hasOpt()) {
1217 vmove = parse.getArg('[', ']');
1218 parse.skip_spaces(true);
1222 colinfo[col].width = width;
1224 cellinfo[row][col].mroffset = vmove;
1225 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1226 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1227 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1228 cellinfo[row][col].mrxnum = ncells - 1;
1231 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1232 if (!cellinfo[row][col].content.empty()) {
1233 // This may or may not work in LaTeX,
1234 // but it does not work in LyX.
1235 // FIXME: Handle it correctly!
1236 cerr << "Moving cell content '"
1238 << "' into a multirow cell. "
1239 "This will probably not work."
1242 cellinfo[row][col].content += os2.str();
1243 } else if (parse.next_token().cs() == "multicolumn") {
1246 size_t const ncells =
1247 convert<unsigned int>(parse.verbatim_item());
1249 // special cell properties alignment
1251 handle_colalign(parse, t, ColInfo());
1252 parse.skip_spaces(true);
1253 ColInfo & ci = t.front();
1255 // The logic of LyX for multicolumn vertical
1256 // lines is too complicated to reproduce it
1257 // here (see LyXTabular::TeXCellPreamble()).
1258 // Therefore we simply put everything in the
1262 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1263 cellinfo[row][col].align = ci.align;
1264 cellinfo[row][col].special = ci.special;
1265 cellinfo[row][col].leftlines = ci.leftlines;
1266 cellinfo[row][col].rightlines = ci.rightlines;
1268 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1269 if (!cellinfo[row][col].content.empty()) {
1270 // This may or may not work in LaTeX,
1271 // but it does not work in LyX.
1272 // FIXME: Handle it correctly!
1273 cerr << "Moving cell content '"
1275 << "' into a multicolumn cell. "
1276 "This will probably not work."
1279 cellinfo[row][col].content += os2.str();
1281 // add dummy cells for multicol
1282 for (size_t i = 0; i < ncells - 1; ++i) {
1284 if (col >= colinfo.size()) {
1285 cerr << "The cell '"
1288 << convert<string>(ncells)
1289 << " columns while the table has only "
1290 << convert<string>(colinfo.size())
1292 << " Therefore the surplus columns will be ignored."
1296 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1297 cellinfo[row][col].align = 'c';
1300 } else if (col == 0 && colinfo.size() > 1 &&
1302 parse.next_token().cs() == "caption") {
1303 // longtable caption support in LyX is a hack:
1304 // Captions require a row of their own with
1305 // the caption flag set to true, having only
1306 // one multicolumn cell. The contents of that
1307 // cell must contain exactly one caption inset
1308 // and nothing else.
1309 // Fortunately, the caption flag is only needed
1310 // for tables with more than one column.
1311 rowinfo[row].caption = true;
1312 for (size_t c = 1; c < cells.size(); ++c) {
1313 if (!cells[c].empty()) {
1314 cerr << "Moving cell content '"
1316 << "' into the caption cell. "
1317 "This will probably not work."
1319 cells[0] += cells[c];
1323 cellinfo[row][col].align = colinfo[col].align;
1324 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1326 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1327 cellinfo[row][col].content += os2.str();
1328 // add dummy multicolumn cells
1329 for (size_t c = 1; c < colinfo.size(); ++c)
1330 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1334 if (parse.next_token().cs() == "begin") {
1335 parse.pushPosition();
1337 string const env = parse.getArg('{', '}');
1338 if (env == "sideways" || env == "turn") {
1339 string angle = "90";
1340 if (env == "turn") {
1342 angle = parse.getArg('{', '}');
1344 active_environments.push_back(env);
1345 parse.ertEnvironment(env);
1346 active_environments.pop_back();
1347 parse.skip_spaces();
1348 if (!parse.good() && support::isStrInt(angle))
1349 rotate = convert<int>(angle);
1351 parse.popPosition();
1353 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1354 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1355 cellinfo[row][col].align = colinfo[col].align;
1358 cellinfo[row][col].rotate = rotate;
1360 active_environments.push_back(parse.getArg('{', '}'));
1362 parse.getArg('{', '}');
1363 parse_text_in_inset(parse, os2, FLAG_END, false, context);
1364 active_environments.pop_back();
1365 preamble.registerAutomaticallyLoadedPackage("rotating");
1367 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1369 cellinfo[row][col].content += os2.str();
1373 //cerr << "// handle almost empty last row what we have\n";
1374 // handle almost empty last row
1375 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1376 //cerr << "remove empty last line\n";
1377 if (rowinfo[row].topline)
1378 rowinfo[row - 1].bottomline = true;
1379 for (size_t col = 0; col < colinfo.size(); ++col)
1380 if (cellinfo[row][col].topline)
1381 cellinfo[row - 1][col].bottomline = true;
1388 // Now we have the table structure and content in rowinfo, colinfo
1390 // Unfortunately LyX has some limitations that we need to work around.
1393 for (size_t row = 0; row < rowinfo.size(); ++row) {
1394 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1395 // Convert cells with special content to multicolumn cells
1396 // (LyX ignores the special field for non-multicolumn cells).
1397 if (cellinfo[row][col].multi == CELL_NORMAL &&
1398 !cellinfo[row][col].special.empty())
1399 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1400 // Add multirow dummy cells
1401 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1402 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1403 && cellinfo[row - 1][col].mrxnum > 0) {
1404 // add dummy cells for multirow
1405 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1406 cellinfo[row][col].align = 'c';
1407 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1412 // Distribute lines from rows/columns to cells
1413 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1414 // Each standard cell inherits the settings of the corresponding
1415 // rowinfo/colinfo. This works because all cells with individual
1416 // settings were converted to multicolumn cells above.
1417 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1418 // corresponding to the first column of the multicolumn cell (default
1419 // of the multicol package). This works because the special field
1420 // overrides the line fields.
1421 for (size_t row = 0; row < rowinfo.size(); ++row) {
1422 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1423 if (cellinfo[row][col].multi == CELL_NORMAL) {
1424 cellinfo[row][col].topline = rowinfo[row].topline;
1425 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1426 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1427 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1428 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1430 while (s < cellinfo[row].size() &&
1431 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1433 if (s < cellinfo[row].size() &&
1434 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1435 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1436 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1437 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1438 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1440 while (s < rowinfo.size() &&
1441 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1443 if (s < cellinfo[row].size() &&
1444 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1445 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1446 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1447 cellinfo[row][col].topline = rowinfo[row].topline;
1453 preamble.registerAutomaticallyLoadedPackage("booktabs");
1454 if (name == "longtable")
1455 preamble.registerAutomaticallyLoadedPackage("longtable");
1456 else if (name == "xltabular")
1457 preamble.registerAutomaticallyLoadedPackage("xltabular");
1458 else if (name == "tabularx")
1459 preamble.registerAutomaticallyLoadedPackage("tabularx");
1461 //cerr << "// output what we have\n";
1462 // output what we have
1463 size_type cols = colinfo.size();
1464 for (size_t col = 0; col < colinfo.size(); ++col) {
1465 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1468 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1469 << "\" columns=\"" << cols << "\">\n";
1471 << write_attribute("rotate", context.tablerotation)
1472 << write_attribute("booktabs", booktabs)
1473 << write_attribute("islongtable", is_long_tabular);
1474 if (is_long_tabular) {
1475 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1476 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1477 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1478 << write_attribute("headTopDL", endhead.topDL)
1479 << write_attribute("headBottomDL", endhead.bottomDL)
1480 << write_attribute("footTopDL", endfoot.topDL)
1481 << write_attribute("footBottomDL", endfoot.bottomDL)
1482 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1483 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1484 << write_attribute("lastFootEmpty", endlastfoot.empty);
1485 if (!halign.empty())
1486 os << write_attribute("longtabularalignment", halign);
1488 os << write_attribute("tabularvalignment", tabularvalignment);
1490 os << write_attribute("tabularwidth", tabularwidth) << ">\n";
1492 //cerr << "// after header\n";
1493 for (size_t col = 0; col < colinfo.size(); ++col) {
1494 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1496 os << "<column alignment=\""
1497 << verbose_align(colinfo[col].align) << "\"";
1498 if (colinfo[col].decimal_point != '\0')
1499 os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
1500 os << " valignment=\""
1501 << verbose_valign(colinfo[col].valign) << "\""
1502 << write_attribute("width", translate_len(colinfo[col].width))
1503 << write_attribute("special", colinfo[col].special)
1504 << write_attribute("varwidth", colinfo[col].varwidth)
1507 //cerr << "// after cols\n";
1509 for (size_t row = 0; row < rowinfo.size(); ++row) {
1511 << write_attribute("topspace", rowinfo[row].top_space)
1512 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1513 << write_attribute("interlinespace", rowinfo[row].interline_space)
1514 << write_attribute("endhead",
1515 rowinfo[row].type == LT_HEAD)
1516 << write_attribute("endfirsthead",
1517 rowinfo[row].type == LT_FIRSTHEAD)
1518 << write_attribute("endfoot",
1519 rowinfo[row].type == LT_FOOT)
1520 << write_attribute("endlastfoot",
1521 rowinfo[row].type == LT_LASTFOOT)
1522 << write_attribute("newpage", rowinfo[row].newpage)
1523 << write_attribute("caption", rowinfo[row].caption)
1525 for (size_t col = 0; col < colinfo.size(); ++col) {
1526 CellInfo const & cell = cellinfo[row][col];
1527 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1528 // These are the second columns in a salign pair. Skip.
1531 if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1532 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1533 && colinfo[col].align != 'd')
1534 os << " multicolumn=\"" << cell.multi << "\"";
1535 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1536 || cell.multi == CELL_PART_OF_MULTIROW)
1537 os << " multirow=\"" << cell.multi << "\"";
1538 os << " alignment=\"" << verbose_align(cell.align)
1540 << " valignment=\"" << verbose_valign(cell.valign)
1542 << write_attribute("topline", cell.topline)
1543 << write_attribute("bottomline", cell.bottomline)
1544 << write_attribute("leftline", cell.leftlines > 0)
1545 << write_attribute("rightline", cell.rightlines > 0)
1546 << write_attribute("rotate", cell.rotate)
1547 << write_attribute("mroffset", cell.mroffset);
1548 //cerr << "\nrow: " << row << " col: " << col;
1550 // cerr << " topline=\"true\"";
1551 //if (cell.bottomline)
1552 // cerr << " bottomline=\"true\"";
1553 os << " usebox=\"none\""
1554 << write_attribute("width", translate_len(cell.width));
1555 if (cell.multi != CELL_NORMAL)
1556 os << write_attribute("special", cell.special);
1558 << "\n\\begin_inset Text\n"
1560 << "\n\\end_inset\n"
1566 os << "</lyxtabular>\n";