racket / scribble

Other
201 stars 92 forks source link

'top cell-property does not work for cells with picts in PDF backend #260

Open wilbowma opened 4 years ago

wilbowma commented 4 years ago

Here's an example:

#lang scribble/base

@tabular[
#:row-properties '((top-border bottom-border))
(list
(list "> " @verbatim|{a very tall

cell.}|))
]

@tabular[
#:row-properties '((top-border top bottom-border))
(list
  (list "> " @verbatim|{a very tall

cell.}|))
]

@(require pict)
@tabular[
#:row-properties '((top-border top bottom-border))
(list
  (list "> " (filled-ellipse 40 40)))
]

Expected output, and the output in HTML: Screenshot_20200812_183954

Actual output in PDF: Screenshot_20200812_184043

wilbowma commented 4 years ago

Looks like the issue is in the way picts are rendered, as includegraphic doesn't have its box adjusted. The solutions I've found by playing with the latex and based on this article https://tex.stackexchange.com/questions/30079/how-to-align-picture-top-left-in-a-table

wilbowma commented 4 years ago

Using adjustbox doesn't quite get is to the same behavior on both backends. In this example, all look the same in HTML, but the first two are slightly different than the second two in PDF. Not sure who is to blame

#lang scribble/base
@tabular[
#:row-properties '((top-border top bottom-border))
(list
(list "> " @verbatim|{a very tall

cell.}|))
]

@(require pict)
@tabular[
#:row-properties '((top-border top bottom-border))
(list
(list "> " (filled-ellipse 40 40)))
]

@tabular[
#:row-properties '((top-border bottom-border))
#:cell-properties '((center top))
(list
(list "> " @verbatim|{a very tall

cell.}|))
]

@(require pict)
@tabular[
#:row-properties '((top-border bottom-border))
#:cell-properties '((center top))
(list
(list "> " (filled-ellipse 40 40)))
]

HTML: image

PDF: image

wilbowma commented 4 years ago

It looks like all cells ought to be wrapped in \begin{tabular}{@{}c@{}}, otherwise they don't seem to have ascent/descent, and modifying their alignment pushes the top of the character, rather than the top of the ascent, to the top of the table. Visually:

image

In my example, the ">" is not wrapped, while the verbatim contents is wrapped.

wilbowma commented 4 years ago

Unfortunately, tables don't seem to align as I expect. The vertical alignment of one cell seems to depend on the other cell, at least when the cell contents is a graphic. In the below example, I try to top align the left and center align the right, then top align both:

image

wilbowma commented 4 years ago

It seems that the vertical alignment of a "cell" is actually aligned with respect to the boxes around it. I have no ability to predict this behavior: image

The latex snippet (which expects to be in the document of a generate scribble template)

\setlength{\fboxsep}{0pt}

\newcommand{\wrap}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}
\newcommand{\id}[1]{#1}

\newcommand{\tablethinggraphic}[4][\id]{
  \vspace{2pt}
  #2 #3 #4
  \vspace{2pt}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hline \fbox{\adjustbox{valign=#2}{#1{\hbox{$>$}}}} &
\fbox{\adjustbox{valign=#3}{{\raisebox{-0.19999999999999574bp}{\makebox[32.00000000000001bp][l]{\includegraphics[trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}}}}
&
\fbox{\adjustbox{valign=#4}{{\raisebox{-0.19999999999999574bp}{\makebox[48.00000000000001bp][l]{\includegraphics[scale=1.5,trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}}}}
\\
\hline \end{bigtabular}
}

\tablethinggraphic[\wrap]{t}{c}{t}
\tablethinggraphic[\wrap]{t}{c}{c}
\tablethinggraphic[\wrap]{t}{t}{b}
\tablethinggraphic[\wrap]{t}{b}{b}
wilbowma commented 4 years ago

Okay I've got an algorithm that I think might work, although so far my implementation isn't working flawlessly: image

Before aligning the cells, we need to measure the height of the row, and the width of each cell. Then we manually build a vertical box around each cell that has the height of the entire row. We add vfil to the beginning and end of the vbox depending on how we want to align it.

\setlength{\fboxsep}{0pt}

\newcommand{\wrap}[2][c]{\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}
\newcommand{\id}[1]{#1}

%require row height and cell
\makeatletter
\newlength{\@meowheight}
\newcommand{\setmeowheight}[1]{
  \settoheight{\@meowheight}{\vbox{#1}}
}

\newcommand{\measurerowheight}[2]{%
  \settoheight{\@meowheight}{\vbox{\begin{bigtabular}{#1}#2{\end{bigtabular}}}}%
  \relax%
}

\newlength{\@meowwidth}
\newcommand{\alignedcell}[2]{%
  \settowidth{\@meowwidth}{#2}
  \vbox to \@meowheight{%
    \ifthenelse{\equal{#1}{c}}{\vfil}{\ifthenelse{\equal{#1}{b}}{\vfil}{\relax}}%
    \hbox to \@meowwidth{#2}%
    \ifthenelse{\equal{#1}{c}}{\vfil}{\ifthenelse{\equal{#1}{t}}{\vfil}{\relax}}%
  }
}
\makeatother

\setmeowheight{
  \begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
    \hline
    \wrap{\hbox{$>$}} &%
    \wrap{\raisebox{-0.19999999999999574bp}{\makebox[32.00000000000001bp][l]{\includegraphics[trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}} &%
    \wrap{\raisebox{-0.19999999999999574bp}{\makebox[48.00000000000001bp][l]{\includegraphics[scale=1.5,trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}}%
    \\ \hline%
    \end{bigtabular}
}
\newcommand{\tablethinggraphic}[4][\id]{
  \vspace{2pt}
  #2 #3 #4
  \vspace{2pt}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
  \hline
  \fbox{\alignedcell{#2}{\wrap{\hbox{$>$}}}} &
  \fbox{\alignedcell{#3}{\wrap{{\raisebox{-0.19999999999999574bp}{\makebox[32.00000000000001bp][l]{\includegraphics[trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}}}}}
  &
  \fbox{\alignedcell{#4}{\wrap{{\raisebox{-0.19999999999999574bp}{\makebox[48.00000000000001bp][l]{\includegraphics[scale=1.5,trim=2.4000000000000004 2.4000000000000004 2.4000000000000004 2.4000000000000004]{pict.pdf}}}}}}}
  \\ \hline
\end{bigtabular}
}

\tablethinggraphic[\wrap]{t}{c}{t}
\tablethinggraphic[\wrap]{t}{c}{c}
\tablethinggraphic[\wrap]{b}{b}{t}
\tablethinggraphic[\wrap]{t}{b}{b}

Not really sure why the alignment seems to affect the size of the vboxs, and I can't see to measure the height of the row while inside the table using this method.