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"
40 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
43 /// vertical alignment
47 /// special column alignment
49 /// number of lines on the right
51 /// number of lines on the left
56 /// row type for longtables
63 /// part of head on first page
67 /// part of foot on last page
74 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
75 caption(false), newpage(false) {}
76 /// Does this row have any special setting?
79 return topline || bottomline || !top_space.empty() ||
80 !bottom_space.empty() || !interline_space.empty() ||
81 type != LT_NORMAL || caption || newpage;
83 /// horizontal line above
85 /// horizontal line below
87 /// Extra space between the top line and this row
89 /// Extra space between this row and the bottom line
91 /// Extra space between the bottom line and the next top line
92 string interline_space;
93 /// These are for longtabulars only
94 /// row type (head, foot, firsthead etc.)
103 /// the numeric values are part of the file format!
107 /// A multicolumn cell. The number of columns is <tt>1 + number
108 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
109 CELL_BEGIN_OF_MULTICOLUMN = 1,
110 /// This is a dummy cell (part of a multicolumn cell)
111 CELL_PART_OF_MULTICOLUMN = 2
117 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
118 leftlines(0), rightlines(0), topline(false),
119 bottomline(false), rotate(0) {}
126 /// vertical cell alignment
128 /// number of lines on the left
130 /// number of lines on the right
132 /// do we have a line above?
134 /// do we have a line below?
136 /// how is the cell rotated?
138 /// width for multicolumn cells
140 /// special formatting for multicolumn cells
148 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
149 // we have this header type (is set in the getLT... functions)
151 // double borders on top
153 // double borders on bottom
155 // used for FirstHeader & LastFooter and if this is true
156 // all the rows marked as FirstHeader or LastFooter are
157 // ignored in the output and it is set to be empty!
162 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
163 inline char const * verbose_align(char c)
178 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
179 inline char const * verbose_valign(char c)
181 // The default value for no special alignment is "top".
194 // stripped down from tabluar.C. We use it currently only for bools and
196 string const write_attribute(string const & name, bool const & b)
198 // we write only true attribute values so we remove a bit of the
199 // file format bloat for tabulars.
200 return b ? ' ' + name + "=\"true\"" : string();
204 string const write_attribute(string const & name, string const & s)
206 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
210 string const write_attribute(string const & name, int const & i)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return i ? write_attribute(name, convert<string>(i)) : string();
218 /*! rather brutish way to code table structure in a string:
223 \multicolumn{2}{c}{4} & 5 //
229 gets "translated" to:
232 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
233 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
234 HLINE 6 TAB 7 HLINE HLINE LINE
235 HLINE 8 HLINE \endhead HLINE LINE
239 char const TAB = '\001';
240 char const LINE = '\002';
241 char const HLINE = '\004';
245 * Move the information in leftlines, rightlines, align and valign to the
246 * special field. This is necessary if the special field is not empty,
247 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
250 void ci2special(ColInfo & ci)
252 if (ci.width.empty() && ci.align == 'n')
253 // The alignment setting is already in special, since
254 // handle_colalign() never stores ci with these settings
255 // and ensures that leftlines == 0 and rightlines == 0 in
259 if (!ci.width.empty()) {
262 ci.special += ">{\\raggedright}";
265 ci.special += ">{\\raggedleft}";
268 ci.special += ">{\\centering}";
271 if (ci.valign == 'n')
274 ci.special += ci.valign;
275 ci.special += '{' + ci.width + '}';
278 ci.special += ci.align;
280 // LyX can only have one left and one right line.
281 for (int i = 1; i < ci.leftlines; ++i)
282 ci.special.insert(0, "|");
283 for (int i = 1; i < ci.rightlines; ++i)
285 ci.leftlines = min(ci.leftlines, 1);
286 ci.rightlines = min(ci.rightlines, 1);
293 * Handle column specifications for tabulars and multicolumns.
294 * The next token of the parser \p p must be an opening brace, and we read
295 * everything until the matching closing brace.
296 * The resulting column specifications are filled into \p colinfo. This is
297 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
299 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
300 ColInfo const & start)
302 if (p.get_token().cat() != catBegin)
303 cerr << "Wrong syntax for table column alignment.\n"
304 "Expected '{', got '" << p.curr_token().asInput()
307 ColInfo next = start;
308 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
311 cerr << "t: " << t << " c: '" << t.character() << "'\n";
314 // We cannot handle comments here
315 if (t.cat() == catComment) {
316 if (t.cs().empty()) {
320 cerr << "Ignoring comment: " << t.asInput();
324 switch (t.character()) {
328 // new column, horizontal aligned
329 next.align = t.character();
330 if (!next.special.empty())
332 colinfo.push_back(next);
338 // new column, vertical aligned box
339 next.valign = t.character();
340 next.width = p.verbatim_item();
341 if (!next.special.empty())
343 colinfo.push_back(next);
348 if (colinfo.empty()) {
349 if (next.special.empty())
353 } else if (colinfo.back().special.empty())
354 ++colinfo.back().rightlines;
355 else if (next.special.empty())
358 colinfo.back().special += '|';
361 // text before the next column
362 string const s = trimSpaceAndEol(p.verbatim_item());
363 if (next.special.empty() &&
365 // Maybe this can be converted to a
366 // horizontal alignment setting for
367 // fixed width columns
368 if (s == "\\raggedleft")
370 else if (s == "\\raggedright")
372 else if (s == "\\centering")
375 next.special = ">{" + s + '}';
377 next.special += ">{" + s + '}';
381 // text after the last column
382 string const s = trimSpaceAndEol(p.verbatim_item());
384 // This is not possible in LaTeX.
385 cerr << "Ignoring separator '<{"
386 << s << "}'." << endl;
388 ColInfo & ci = colinfo.back();
390 ci.special += "<{" + s + '}';
395 // *{n}{arg} means 'n' columns of type 'arg'
396 string const num = p.verbatim_item();
397 string const arg = p.verbatim_item();
398 size_t const n = convert<unsigned int>(num);
399 if (!arg.empty() && n > 0) {
401 for (size_t i = 0; i < n; ++i)
405 handle_colalign(p2, colinfo, next);
408 cerr << "Ignoring column specification"
409 " '*{" << num << "}{"
410 << arg << "}'." << endl;
415 // text instead of the column spacing
417 // text in addition to the column spacing
418 next.special += t.character();
419 next.special += '{' + p.verbatim_item() + '}';
422 // try user defined column types
423 // unknown column types (nargs == -1) are
424 // assumed to consume no arguments
426 next.special += t.character();
428 preamble.getSpecialTableColumnArguments(t.character());
429 for (int i = 0; i < nargs; ++i)
430 next.special += '{' +
431 p.verbatim_item() + '}';
432 colinfo.push_back(next);
439 // Maybe we have some column separators that need to be added to the
442 if (!next.special.empty()) {
443 ColInfo & ci = colinfo.back();
445 ci.special += next.special;
446 next.special.erase();
452 * Move the left and right lines and alignment settings of the column \p ci
453 * to the special field if necessary.
455 void fix_colalign(ColInfo & ci)
457 if (ci.leftlines > 1 || ci.rightlines > 1)
463 * LyX can't handle more than one vertical line at the left or right side
465 * This function moves the left and right lines and alignment settings of all
466 * columns in \p colinfo to the special field if necessary.
468 void fix_colalign(vector<ColInfo> & colinfo)
470 // Try to move extra leftlines to the previous column.
471 // We do this only if both special fields are empty, otherwise we
472 // can't tell wether the result will be the same.
473 for (size_t col = 0; col < colinfo.size(); ++col) {
474 if (colinfo[col].leftlines > 1 &&
475 colinfo[col].special.empty() && col > 0 &&
476 colinfo[col - 1].rightlines == 0 &&
477 colinfo[col - 1].special.empty()) {
478 ++colinfo[col - 1].rightlines;
479 --colinfo[col].leftlines;
482 // Try to move extra rightlines to the next column
483 for (size_t col = 0; col < colinfo.size(); ++col) {
484 if (colinfo[col].rightlines > 1 &&
485 colinfo[col].special.empty() &&
486 col < colinfo.size() - 1 &&
487 colinfo[col + 1].leftlines == 0 &&
488 colinfo[col + 1].special.empty()) {
489 ++colinfo[col + 1].leftlines;
490 --colinfo[col].rightlines;
493 // Move the lines and alignment settings to the special field if
495 for (size_t col = 0; col < colinfo.size(); ++col)
496 fix_colalign(colinfo[col]);
501 * Parse hlines and similar stuff.
502 * \returns wether the token \p t was parsed
504 bool parse_hlines(Parser & p, Token const & t, string & hlines,
505 bool is_long_tabular)
507 LASSERT(t.cat() == catEscape, return false);
509 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
510 t.cs() == "bottomrule")
511 hlines += '\\' + t.cs();
513 else if (t.cs() == "cline")
514 hlines += "\\cline{" + p.verbatim_item() + '}';
516 else if (t.cs() == "cmidrule") {
517 // We cannot handle the \cmidrule(l){3-4} form
520 bool const hasParentheses(p.getFullArg('(', ')').first);
524 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
527 else if (t.cs() == "addlinespace") {
530 bool const hasArgument(p.getFullArg('{', '}').first);
533 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
535 hlines += "\\addlinespace";
538 else if (is_long_tabular && t.cs() == "newpage")
539 hlines += "\\newpage";
548 /// Position in a row
550 /// At the very beginning, before the first token
552 /// After the first token and before any column token
554 /// After the first column token. Comments and whitespace are only
555 /// treated as tokens in this position
557 /// After the first non-column token at the end
563 * Parse table structure.
564 * We parse tables in a two-pass process: This function extracts the table
565 * structure (rows, columns, hlines etc.), but does not change the cell
566 * content. The cell content is parsed in a second step in handle_tabular().
568 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
569 RowPosition & pos, unsigned flags)
571 // table structure commands such as \hline
574 // comments that occur at places where we can't handle them
578 Token const & t = p.get_token();
581 debugToken(cerr, t, flags);
584 // comments and whitespace in hlines
587 case IN_HLINES_START:
589 if (t.cat() == catComment) {
594 // We can't handle comments here,
595 // store them for later use
596 comments += t.asInput();
598 } else if (t.cat() == catSpace ||
599 t.cat() == catNewline) {
600 // whitespace is irrelevant here, we
601 // need to recognize hline stuff
610 // We need to handle structure stuff first in order to
611 // determine wether we need to output a HLINE separator
612 // before the row or not.
613 if (t.cat() == catEscape) {
614 if (parse_hlines(p, t, hlines, is_long_tabular)) {
617 pos = IN_HLINES_START;
622 case IN_HLINES_START:
629 else if (t.cs() == "tabularnewline" ||
633 cerr << "Warning: Converting TeX "
634 "'\\cr' to LaTeX '\\\\'."
636 // stuff before the line break
637 os << comments << HLINE << hlines << HLINE
639 //cerr << "hlines: " << hlines << endl;
646 else if (is_long_tabular &&
647 (t.cs() == "endhead" ||
648 t.cs() == "endfirsthead" ||
649 t.cs() == "endfoot" ||
650 t.cs() == "endlastfoot")) {
651 hlines += t.asInput();
655 // these commands are implicit line
657 os << comments << HLINE << hlines
664 pos = IN_HLINES_START;
666 case IN_HLINES_START:
673 // We need a HLINE separator if we either have no hline
674 // stuff at all and are just starting a row or if we just
675 // got the first non-hline token.
678 // no hline tokens exist, first token at row start
679 case IN_HLINES_START:
680 // hline tokens exist, first non-hline token at row
682 os << hlines << HLINE << comments;
688 // Oops, there is still cell content or unsupported
689 // booktabs commands after hline stuff. The latter are
690 // moved to the cell, and the first does not work in
691 // LaTeX, so we ignore the hlines.
694 if (support::contains(hlines, "\\hline") ||
695 support::contains(hlines, "\\cline") ||
696 support::contains(hlines, "\\newpage"))
697 cerr << "Ignoring '" << hlines
698 << "' in a cell" << endl;
708 // If we come here we have normal cell content
712 if (t.cat() == catMath) {
713 // we are inside some text mode thingy, so opening new math is allowed
714 Token const & n = p.get_token();
715 if (n.cat() == catMath) {
716 // TeX's $$...$$ syntax for displayed math
718 // This does only work because parse_math outputs TeX
719 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
721 p.get_token(); // skip the second '$' token
723 // simple $...$ stuff
726 // This does only work because parse_math outputs TeX
727 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
732 else if (t.cat() == catSpace
733 || t.cat() == catNewline
734 || t.cat() == catLetter
735 || t.cat() == catSuper
737 || t.cat() == catOther
738 || t.cat() == catActive
739 || t.cat() == catParameter)
742 else if (t.cat() == catBegin) {
744 parse_table(p, os, is_long_tabular, pos,
749 else if (t.cat() == catEnd) {
750 if (flags & FLAG_BRACE_LAST)
752 cerr << "unexpected '}'\n";
755 else if (t.cat() == catAlign) {
760 else if (t.cat() == catComment)
763 else if (t.cs() == "(") {
765 // This does only work because parse_math outputs TeX
766 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
770 else if (t.cs() == "[") {
772 // This does only work because parse_math outputs TeX
773 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
777 else if (t.cs() == "begin") {
778 string const name = p.getArg('{', '}');
779 active_environments.push_back(name);
780 os << "\\begin{" << name << '}';
781 // treat the nested environment as a block, don't
782 // parse &, \\ etc, because they don't belong to our
783 // table if they appear.
784 os << p.ertEnvironment(name);
785 os << "\\end{" << name << '}';
786 active_environments.pop_back();
789 else if (t.cs() == "end") {
790 if (flags & FLAG_END) {
791 // eat environment name
792 string const name = p.getArg('{', '}');
793 if (name != active_environment())
794 p.error("\\end{" + name + "} does not match \\begin{"
795 + active_environment() + "}");
798 p.error("found 'end' unexpectedly");
805 // We can have comments if the last line is incomplete
808 // We can have hline stuff if the last line is incomplete
809 if (!hlines.empty()) {
810 // this does not work in LaTeX, so we ignore it
811 cerr << "Ignoring '" << hlines << "' at end of tabular"
817 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
820 for (size_t col = 0; col < ci.size(); ++col)
821 ci[col].topline = true;
825 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
827 ri.bottomline = true;
828 for (size_t col = 0; col < ci.size(); ++col)
829 ci[col].bottomline = true;
833 } // anonymous namespace
836 void handle_tabular(Parser & p, ostream & os, string const & name,
837 string const & tabularwidth, Context & context)
839 bool const is_long_tabular(name == "longtable");
840 bool booktabs = false;
841 string tabularvalignment("middle");
842 string posopts = p.getOpt();
843 if (!posopts.empty()) {
844 // FIXME: Convert this to ERT
846 cerr << "horizontal longtable positioning '"
847 << posopts << "' ignored\n";
848 else if (posopts == "[t]")
849 tabularvalignment = "top";
850 else if (posopts == "[b]")
851 tabularvalignment = "bottom";
853 cerr << "vertical tabular positioning '"
854 << posopts << "' ignored\n";
857 vector<ColInfo> colinfo;
859 // handle column formatting
860 handle_colalign(p, colinfo, ColInfo());
861 fix_colalign(colinfo);
863 // first scan of cells
864 // use table mode to keep it minimal-invasive
865 // not exactly what's TeX doing...
866 vector<string> lines;
868 RowPosition rowpos = ROW_START;
869 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
870 split(ss.str(), lines, LINE);
872 vector< vector<CellInfo> > cellinfo(lines.size());
873 vector<RowInfo> rowinfo(lines.size());
880 //cerr << "// split into rows\n";
881 for (size_t row = 0; row < rowinfo.size();) {
884 cellinfo[row].resize(colinfo.size());
885 bool deletelastrow = false;
888 vector<string> dummy;
889 //cerr << "\n########### LINE: " << lines[row] << "########\n";
890 split(lines[row], dummy, HLINE);
892 // handle horizontal line fragments
893 // we do only expect this for a last line without '\\'
894 if (dummy.size() != 3) {
895 if ((dummy.size() != 1 && dummy.size() != 2) ||
896 row != rowinfo.size() - 1)
897 cerr << "unexpected dummy size: " << dummy.size()
898 << " content: " << lines[row] << "\n";
901 lines[row] = dummy[1];
903 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
904 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
905 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
907 for (int i = 0; i <= 2; i += 2) {
908 //cerr << " reading from line string '" << dummy[i] << "'\n";
911 Token t = p1.get_token();
912 //cerr << "read token: " << t << "\n";
913 if (t.cs() == "hline" || t.cs() == "toprule" ||
914 t.cs() == "midrule" ||
915 t.cs() == "bottomrule") {
916 if (t.cs() != "hline")
919 if (rowinfo[row].topline) {
920 if (row > 0) // extra bottomline above
921 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
923 cerr << "dropping extra "
925 //cerr << "below row: " << row-1 << endl;
927 handle_hline_above(rowinfo[row], cellinfo[row]);
928 //cerr << "above row: " << row << endl;
931 //cerr << "below row: " << row << endl;
932 handle_hline_below(rowinfo[row], cellinfo[row]);
934 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
935 if (t.cs() == "cmidrule")
937 string arg = p1.verbatim_item();
938 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
940 split(arg, cols, '-');
942 size_t from = convert<unsigned int>(cols[0]);
944 cerr << "Could not parse "
945 << t.cs() << " start column."
948 // 1 based index -> 0 based
950 if (from >= colinfo.size()) {
951 cerr << t.cs() << " starts at "
952 "non existing column "
953 << (from + 1) << endl;
954 from = colinfo.size() - 1;
956 size_t to = convert<unsigned int>(cols[1]);
958 cerr << "Could not parse "
959 << t.cs() << " end column."
962 // 1 based index -> 0 based
964 if (to >= colinfo.size()) {
965 cerr << t.cs() << " ends at "
966 "non existing column "
968 to = colinfo.size() - 1;
970 for (size_t col = from; col <= to; ++col) {
971 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
973 rowinfo[row].topline = true;
974 cellinfo[row][col].topline = true;
976 rowinfo[row].bottomline = true;
977 cellinfo[row][col].bottomline = true;
980 } else if (t.cs() == "addlinespace") {
982 string const opt = p.next_token().cat() == catBegin ?
983 p.verbatim_item() : string();
986 rowinfo[row].top_space = "default";
988 rowinfo[row].top_space = translate_len(opt);
989 } else if (rowinfo[row].bottomline) {
991 rowinfo[row].bottom_space = "default";
993 rowinfo[row].bottom_space = translate_len(opt);
996 rowinfo[row].interline_space = "default";
998 rowinfo[row].interline_space = translate_len(opt);
1000 } else if (t.cs() == "endhead") {
1002 endhead.empty = true;
1004 rowinfo[row].type = LT_HEAD;
1005 for (int r = row - 1; r >= 0; --r) {
1006 if (rowinfo[r].type != LT_NORMAL)
1008 rowinfo[r].type = LT_HEAD;
1009 endhead.empty = false;
1012 } else if (t.cs() == "endfirsthead") {
1014 endfirsthead.empty = true;
1016 rowinfo[row].type = LT_FIRSTHEAD;
1017 for (int r = row - 1; r >= 0; --r) {
1018 if (rowinfo[r].type != LT_NORMAL)
1020 rowinfo[r].type = LT_FIRSTHEAD;
1021 endfirsthead.empty = false;
1023 endfirsthead.set = true;
1024 } else if (t.cs() == "endfoot") {
1026 endfoot.empty = true;
1028 rowinfo[row].type = LT_FOOT;
1029 for (int r = row - 1; r >= 0; --r) {
1030 if (rowinfo[r].type != LT_NORMAL)
1032 rowinfo[r].type = LT_FOOT;
1033 endfoot.empty = false;
1036 } else if (t.cs() == "endlastfoot") {
1038 endlastfoot.empty = true;
1040 rowinfo[row].type = LT_LASTFOOT;
1041 for (int r = row - 1; r >= 0; --r) {
1042 if (rowinfo[r].type != LT_NORMAL)
1044 rowinfo[r].type = LT_LASTFOOT;
1045 endlastfoot.empty = false;
1047 endlastfoot.set = true;
1048 } else if (t.cs() == "newpage") {
1051 rowinfo[row - 1].newpage = true;
1053 // This does not work in LaTeX
1059 rowinfo[row].newpage = true;
1061 cerr << "unexpected line token: " << t << endl;
1066 // LyX ends headers and footers always with \tabularnewline.
1067 // This causes one additional row in the output.
1068 // If the last row of a header/footer is empty, we can work
1069 // around that by removing it.
1071 RowInfo test = rowinfo[row-1];
1072 test.type = LT_NORMAL;
1073 if (lines[row-1].empty() && !test.special()) {
1074 switch (rowinfo[row-1].type) {
1076 if (rowinfo[row].type != LT_FIRSTHEAD &&
1077 rowinfo[row-2].type == LT_FIRSTHEAD)
1078 deletelastrow = true;
1081 if (rowinfo[row].type != LT_HEAD &&
1082 rowinfo[row-2].type == LT_HEAD)
1083 deletelastrow = true;
1086 if (rowinfo[row].type != LT_FOOT &&
1087 rowinfo[row-2].type == LT_FOOT)
1088 deletelastrow = true;
1091 if (rowinfo[row].type != LT_LASTFOOT &&
1092 rowinfo[row-2].type == LT_LASTFOOT)
1093 deletelastrow = true;
1101 if (deletelastrow) {
1102 lines.erase(lines.begin() + (row - 1));
1103 rowinfo.erase(rowinfo.begin() + (row - 1));
1104 cellinfo.erase(cellinfo.begin() + (row - 1));
1109 vector<string> cells;
1110 split(lines[row], cells, TAB);
1111 for (size_t col = 0, cell = 0; cell < cells.size();
1113 //cerr << "cell content: '" << cells[cell] << "'\n";
1114 if (col >= colinfo.size()) {
1115 // This does not work in LaTeX
1116 cerr << "Ignoring extra cell '"
1117 << cells[cell] << "'." << endl;
1120 Parser p(cells[cell]);
1122 //cells[cell] << "'\n";
1123 if (p.next_token().cs() == "multicolumn") {
1126 size_t const ncells =
1127 convert<unsigned int>(p.verbatim_item());
1129 // special cell properties alignment
1131 handle_colalign(p, t, ColInfo());
1132 p.skip_spaces(true);
1133 ColInfo & ci = t.front();
1135 // The logic of LyX for multicolumn vertical
1136 // lines is too complicated to reproduce it
1137 // here (see LyXTabular::TeXCellPreamble()).
1138 // Therefore we simply put everything in the
1142 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1143 cellinfo[row][col].align = ci.align;
1144 cellinfo[row][col].special = ci.special;
1145 cellinfo[row][col].leftlines = ci.leftlines;
1146 cellinfo[row][col].rightlines = ci.rightlines;
1148 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1149 if (!cellinfo[row][col].content.empty()) {
1150 // This may or may not work in LaTeX,
1151 // but it does not work in LyX.
1152 // FIXME: Handle it correctly!
1153 cerr << "Moving cell content '"
1155 << "' into a multicolumn cell. "
1156 "This will probably not work."
1159 cellinfo[row][col].content += os.str();
1161 // add dummy cells for multicol
1162 for (size_t i = 0; i < ncells - 1; ++i) {
1164 if (col >= colinfo.size()) {
1165 cerr << "The cell '"
1168 << convert<string>(ncells)
1169 << " columns while the table has only "
1170 << convert<string>(colinfo.size())
1172 << " Therefore the surplus columns will be ignored."
1176 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1177 cellinfo[row][col].align = 'c';
1180 } else if (col == 0 && colinfo.size() > 1 &&
1182 p.next_token().cs() == "caption") {
1183 // longtable caption support in LyX is a hack:
1184 // Captions require a row of their own with
1185 // the caption flag set to true, having only
1186 // one multicolumn cell. The contents of that
1187 // cell must contain exactly one caption inset
1188 // and nothing else.
1189 // Fortunately, the caption flag is only needed
1190 // for tables with more than one column.
1191 rowinfo[row].caption = true;
1192 for (size_t c = 1; c < cells.size(); ++c) {
1193 if (!cells[c].empty()) {
1194 cerr << "Moving cell content '"
1196 << "' into the caption cell. "
1197 "This will probably not work."
1199 cells[0] += cells[c];
1203 cellinfo[row][col].align = colinfo[col].align;
1204 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1206 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1207 cellinfo[row][col].content += os.str();
1208 // add dummy multicolumn cells
1209 for (size_t c = 1; c < colinfo.size(); ++c)
1210 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1214 if (p.next_token().cs() == "begin") {
1217 string const env = p.getArg('{', '}');
1218 if (env == "sideways" || env == "turn") {
1219 string angle = "90";
1220 if (env == "turn") {
1222 angle = p.getArg('{', '}');
1224 active_environments.push_back(env);
1225 p.ertEnvironment(env);
1226 active_environments.pop_back();
1228 if (!p.good() && support::isStrInt(angle))
1229 rotate = convert<int>(angle);
1233 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1234 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1235 cellinfo[row][col].align = colinfo[col].align;
1238 cellinfo[row][col].rotate = rotate;
1240 active_environments.push_back(p.getArg('{', '}'));
1243 parse_text_in_inset(p, os, FLAG_END, false, context);
1244 active_environments.pop_back();
1245 preamble.registerAutomaticallyLoadedPackage("rotating");
1247 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1249 cellinfo[row][col].content += os.str();
1253 //cerr << "// handle almost empty last row what we have\n";
1254 // handle almost empty last row
1255 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1256 //cerr << "remove empty last line\n";
1257 if (rowinfo[row].topline)
1258 rowinfo[row - 1].bottomline = true;
1259 for (size_t col = 0; col < colinfo.size(); ++col)
1260 if (cellinfo[row][col].topline)
1261 cellinfo[row - 1][col].bottomline = true;
1268 // Now we have the table structure and content in rowinfo, colinfo
1270 // Unfortunately LyX has some limitations that we need to work around.
1272 // Convert cells with special content to multicolumn cells
1273 // (LyX ignores the special field for non-multicolumn cells).
1274 for (size_t row = 0; row < rowinfo.size(); ++row) {
1275 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1276 if (cellinfo[row][col].multi == CELL_NORMAL &&
1277 !cellinfo[row][col].special.empty())
1278 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1282 // Distribute lines from rows/columns to cells
1283 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1284 // Each standard cell inherits the settings of the corresponding
1285 // rowinfo/colinfo. This works because all cells with individual
1286 // settings were converted to multicolumn cells above.
1287 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1288 // corresponding to the first column of the multicolumn cell (default
1289 // of the multicol package). This works because the special field
1290 // overrides the line fields.
1291 for (size_t row = 0; row < rowinfo.size(); ++row) {
1292 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1293 if (cellinfo[row][col].multi == CELL_NORMAL) {
1294 cellinfo[row][col].topline = rowinfo[row].topline;
1295 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1296 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1297 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1298 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1300 while (s < cellinfo[row].size() &&
1301 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1303 if (s < cellinfo[row].size() &&
1304 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1305 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1306 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1307 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1313 preamble.registerAutomaticallyLoadedPackage("booktabs");
1314 if (is_long_tabular)
1315 preamble.registerAutomaticallyLoadedPackage("longtable");
1317 //cerr << "// output what we have\n";
1318 // output what we have
1319 string const rotate = "0";
1320 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1321 << "\" columns=\"" << colinfo.size() << "\">\n";
1323 << write_attribute("rotate", rotate)
1324 << write_attribute("booktabs", booktabs)
1325 << write_attribute("islongtable", is_long_tabular);
1326 if (is_long_tabular) {
1327 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1328 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1329 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1330 << write_attribute("headTopDL", endhead.topDL)
1331 << write_attribute("headBottomDL", endhead.bottomDL)
1332 << write_attribute("footTopDL", endfoot.topDL)
1333 << write_attribute("footBottomDL", endfoot.bottomDL)
1334 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1335 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1336 << write_attribute("lastFootEmpty", endlastfoot.empty);
1338 os << write_attribute("tabularvalignment", tabularvalignment)
1339 << write_attribute("tabularwidth", tabularwidth);
1342 //cerr << "// after header\n";
1343 for (size_t col = 0; col < colinfo.size(); ++col) {
1344 os << "<column alignment=\""
1345 << verbose_align(colinfo[col].align) << "\""
1347 << verbose_valign(colinfo[col].valign) << "\""
1348 << write_attribute("width", translate_len(colinfo[col].width))
1349 << write_attribute("special", colinfo[col].special)
1352 //cerr << "// after cols\n";
1354 for (size_t row = 0; row < rowinfo.size(); ++row) {
1356 << write_attribute("topspace", rowinfo[row].top_space)
1357 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1358 << write_attribute("interlinespace", rowinfo[row].interline_space)
1359 << write_attribute("endhead",
1360 rowinfo[row].type == LT_HEAD)
1361 << write_attribute("endfirsthead",
1362 rowinfo[row].type == LT_FIRSTHEAD)
1363 << write_attribute("endfoot",
1364 rowinfo[row].type == LT_FOOT)
1365 << write_attribute("endlastfoot",
1366 rowinfo[row].type == LT_LASTFOOT)
1367 << write_attribute("newpage", rowinfo[row].newpage)
1368 << write_attribute("caption", rowinfo[row].caption)
1370 for (size_t col = 0; col < colinfo.size(); ++col) {
1371 CellInfo const & cell = cellinfo[row][col];
1373 if (cell.multi != CELL_NORMAL)
1374 os << " multicolumn=\"" << cell.multi << "\"";
1375 os << " alignment=\"" << verbose_align(cell.align)
1377 << " valignment=\"" << verbose_valign(cell.valign)
1379 << write_attribute("topline", cell.topline)
1380 << write_attribute("bottomline", cell.bottomline)
1381 << write_attribute("leftline", cell.leftlines > 0)
1382 << write_attribute("rightline", cell.rightlines > 0)
1383 << write_attribute("rotate", cell.rotate);
1384 //cerr << "\nrow: " << row << " col: " << col;
1386 // cerr << " topline=\"true\"";
1387 //if (cell.bottomline)
1388 // cerr << " bottomline=\"true\"";
1389 os << " usebox=\"none\""
1390 << write_attribute("width", translate_len(cell.width));
1391 if (cell.multi != CELL_NORMAL)
1392 os << write_attribute("special", cell.special);
1394 << "\n\\begin_inset Text\n"
1396 << "\n\\end_inset\n"
1402 os << "</lyxtabular>\n";