Skip to content

Commit d6e05af

Browse files
committed
I have noticed that the latex format in psql has some bugs:
o "_" is not escaped, and causes TeX to abort, thinking it's a subscript outside of maths mode. Most of my table and field names use underscores, so this is a really nasty one. o The column count is calculated using the contents of opt_align. But opt_align has one extra element, and so it's always one too many. I changed it to count the column headings, like all the other output formats. There may be a bug in computing opt_align that this patch does not address, but I'm not yet familiar enough with the psql source to fix this as well. o The line drawing rules for each border setting (0-3) and expanded mode didn't always match the documented behaviour and what other formats (e.g. aligned) did. I made it as conformant as possible, and also tidied the alignment of the first line of the footer, which was incorrectly indented. Roger Leigh
1 parent 7f018ac commit d6e05af

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/bin/psql/print.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.48 2004/05/23 22:20:10 neilc Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.49 2004/08/06 18:09:15 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -769,7 +769,7 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
769769

770770

771771
/*************************/
772-
/* LaTeX */
772+
/* LaTeX */
773773
/*************************/
774774

775775

@@ -790,6 +790,9 @@ latex_escaped_print(const char *in, FILE *fout)
790790
case '$':
791791
fputs("\\$", fout);
792792
break;
793+
case '_':
794+
fputs("\\_", fout);
795+
break;
793796
case '{':
794797
fputs("\\{", fout);
795798
break;
@@ -817,7 +820,6 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
817820
{
818821
unsigned int col_count = 0;
819822
unsigned int i;
820-
const char *cp;
821823
const char *const * ptr;
822824

823825

@@ -829,42 +831,39 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
829831
fputs("\n\\end{center}\n\n", fout);
830832
}
831833

834+
/* count columns */
835+
for (ptr = headers; *ptr; ptr++)
836+
col_count++;
837+
832838
/* begin environment and set alignments and borders */
833839
fputs("\\begin{tabular}{", fout);
834-
if (opt_border == 0)
835-
fputs(opt_align, fout);
836-
else if (opt_border == 1)
837-
{
838-
for (cp = opt_align; *cp; cp++)
839-
{
840-
if (cp != opt_align)
841-
fputc('|', fout);
842-
fputc(*cp, fout);
843-
}
844-
}
845-
else if (opt_border == 2)
840+
841+
if (opt_border == 2)
842+
fputs("| ", fout);
843+
for (i = 0; i < col_count; i++)
846844
{
847-
for (cp = opt_align; *cp; cp++)
848-
{
849-
fputc('|', fout);
850-
fputc(*cp, fout);
851-
}
852-
fputc('|', fout);
845+
fputc(*(opt_align + i), fout);
846+
if (opt_border != 0 && i < col_count - 1)
847+
fputs (" | ", fout);
853848
}
849+
if (opt_border == 2)
850+
fputs(" |", fout);
851+
854852
fputs("}\n", fout);
855853

856854
if (!opt_barebones && opt_border == 2)
857855
fputs("\\hline\n", fout);
858856

859857
/* print headers and count columns */
860-
for (i = 0, ptr = headers; *ptr; i++, ptr++)
858+
for (i = 0, ptr = headers; i < col_count; i++, ptr++)
861859
{
862-
col_count++;
863860
if (!opt_barebones)
864861
{
865862
if (i != 0)
866863
fputs(" & ", fout);
864+
fputs("\\textit{", fout);
867865
latex_escaped_print(*ptr, fout);
866+
fputc('}', fout);
868867
}
869868
}
870869

@@ -888,7 +887,7 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
888887
if (opt_border == 2)
889888
fputs("\\hline\n", fout);
890889

891-
fputs("\\end{tabular}\n\n", fout);
890+
fputs("\\end{tabular}\n\n\\noindent ", fout);
892891

893892

894893
/* print footers */
@@ -951,8 +950,12 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
951950
if (!opt_barebones)
952951
{
953952
if (opt_border == 2)
953+
{
954954
fputs("\\hline\n", fout);
955-
fprintf(fout, "\\multicolumn{2}{c}{Record %d} \\\\\n", record++);
955+
fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %d}} \\\\\n", record++);
956+
}
957+
else
958+
fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %d}} \\\\\n", record++);
956959
}
957960
if (opt_border >= 1)
958961
fputs("\\hline\n", fout);
@@ -967,7 +970,7 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
967970
if (opt_border == 2)
968971
fputs("\\hline\n", fout);
969972

970-
fputs("\\end{tabular}\n\n", fout);
973+
fputs("\\end{tabular}\n\n\\noindent ", fout);
971974

972975

973976
/* print footers */

0 commit comments

Comments
 (0)