WeBWorK Main Forum

DataTable vertical alignment does not show in hardcopy

DataTable vertical alignment does not show in hardcopy

by Andras Balogh -
Number of replies: 2

I am trying to use a Datatable in order to place a Datatable and a tikz figure next to each other, while aligned vertically in the middle. The alignment seem to work nicely but in screen rendering, but not in hardcopy. In hardcopy the top of the table starts in the middle of the figure. I am not sure how to fix this. The sample code is below.

DOCUMENT();

loadMacros(
    "PGstandard.pl",   
    "PGML.pl",         
    "PGcourse.pl",     
    "niceTables.pl",
    "PGtikz.pl"
);

$graph_image = createTikZImage();
$graph_image->texPackages(["pgfplots"]);
$graph_image->addToPreamble("\usepgfplotslibrary{fillbetween}");
$graph_image->BEGIN_TIKZ
\begin{axis}[ 
    grid=both,
    xmin=-4, xmax=4,
    ymin=-7, ymax=7,
    xtick distance=1, ytick distance=1, 
    axis lines = middle, xlabel=\(x\),ylabel=\(y\)
]

\end{axis}
END_TIKZ


BEGIN_PGML

Sketch the graph of a function that has the following table of values:


[@ DataTable([
[  PGML("[@ DataTable([
    [[PGML('[`x`]'), headerrow => 1], PGML('[`f(x)`]')],
    [PGML('[`-2`]'), PGML('[`0.5`]')],
    [PGML('[`-1`]'), PGML('[`2.5`]')],
    [PGML('[`0`]'), PGML('[`1`]')],
    [PGML('[`1`]'), PGML('[`-1`]')],
    [PGML('[`2`]'), PGML('[`-3.5`]')],
  ],  align => '|c|c|', horizontalrules => 1);
  @]*"), 
  PGML(' [@ image($graph_image, width => 300, tex_size => 300) @]*')]
], allcellcss => 'vertical-align: middle; ') @]*

END_PGML

ENDDOCUMENT();

In reply to Andras Balogh

DataTable vertical alignment does not show in hardcopy

by Alex Jordan -

A few comments before I get to a (not the best) answer. Your inner table is indeed a data table. But your outer table is just being used for layout. The output will be more web accessible if you use LayoutTable instead of DataTable for the outer table.

There is a lot of clutter in your code. In the last two or three versions of PG, we've made it possible to mark up everything for this using PGML. I'll paste the translated version below. It should work for 2.19 and later, if not also earlier. I forget exactly when we added support of tables and images.

Some trivia: PGML.pl automatically loads niceTables.pl, so that can be taken out of your macros.

Now to the answer. A LaTeX tabular cannot do the kind of alignment you are asking for without using m{width} as the column type. A width has to be declared for the column. This created a difficulty for supporting middle alignment in the way you want to use it. The closest thing that works now (that I could see this evening) is to use p{width} for the column types on the outer table. With a width in play, it can honor the valign => 'middle'. The result is maybe less bad, but still doesn't look right. So this feature just isn't working and it's not well documented that it isn't working. And it's not clear yet how to address the issue about needing a width to be declared.

In the below, [# ... #] is for a DataTable and [# ... #]* is for a LayoutTable. For more details about the PGML markup, go to the problem editor and click the PGML button near the top of the editor.

DOCUMENT();

loadMacros(
    "PGstandard.pl",   
    "PGML.pl",         
    "PGcourse.pl",     
    "PGtikz.pl"
);

$graph_image = createTikZImage();
$graph_image->texPackages(["pgfplots"]);
$graph_image->addToPreamble("\usepgfplotslibrary{fillbetween}");
$graph_image->BEGIN_TIKZ
\begin{axis}[ 
    grid=both,
    xmin=-4, xmax=4,
    ymin=-7, ymax=7,
    xtick distance=1, ytick distance=1, 
    axis lines = middle, xlabel=\(x\),ylabel=\(y\)
]

\end{axis}
END_TIKZ


BEGIN_PGML

Sketch the graph of a function that has the following table of values:

[#
    [.
        [#
            [.x.]{headerrow => 1} [.f(x).]*
            [.-2.] [.0.5.]*
            [.-1.] [.2.5.]*
            [.0.]  [.1.]*
            [.1.]  [.-1.]*
            [.2.]  [.-3.5.]
        #]{align => '|c|c|', horizontalrules => 1, encase => [$BM,$EM]}
    .]
    [.
        [!alt text!]{$graph_image}{300}
    .]
#]*{align => 'p{1in}p{3in}', valign => 'middle'}

END_PGML

ENDDOCUMENT();

In reply to Alex Jordan

DataTable vertical alignment does not show in hardcopy

by Andras Balogh -

Great! Thanks! I did not know about the excellent documentation inside the editor.

I vaguely remembered the need of fixed-length columns for vertical alignment in LaTeX, and I was very close to trying it, but I got distracted by the alignment working in HTML.