Difference between revisions of "Vertically Centered Boxes"

From Wiki
Jump to navigation Jump to search
m (Text replacement - "</cmd>" to "}}")
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
===In LaTeX===
+
== LaTeX==
 +
 
 +
In LaTeX, it's fairly easy to produce vertically centered boxes, using optional arguments to either the <code>\parbox</code> command or the <code>tabular</code> environment, as in this example.
 +
 
 
<texcode>
 
<texcode>
 
\documentclass{article}
 
\documentclass{article}
Line 9: Line 12:
 
</texcode>
 
</texcode>
  
===In ConTeXt: ???===
+
== ConTeXt ==
 
 
Please note that this is only my suggestion. I believe there must be an easier way too!
 
  
For the first box:
+
In ConTeXt, the {{cmd|framed}} command can be used to produce similar results.  To produce exactly the same output as the LaTeX commands requires the specification of rather more options, but this is mainly because the defaults are different.
  
 
<texcode>
 
<texcode>
\setupTABLE[frame=off]
+
a box with a table:
\setupTABLE[c][1,2][align=lohi]
+
\framed
\bTABLE
+
  [location=middle, % vertically centered
\bTR\bTD
+
    align=flushleft, % align=no (default) is a normal hbox
a box with a table:
+
                    % align=flushleft/middle/flushright/normal is used for multiple lines
\eTD\bTD[frame=on]
+
    offset=5pt]     % to make the frame wider (as in LaTeX)
{\bTABLE[frame=off]
+
  {1\\2\\3}
\bTR\bTD 1 \eTD\eTR
 
\bTR\bTD 2 \eTD\eTR
 
\bTR\bTD 3 \eTD\eTR
 
\eTABLE}
 
\eTD\eTR
 
\eTABLE
 
</texcode>
 
  
or
+
a midaligned parbox:
 
+
\framed
<texcode>
+
  [location=middle, % vertically centered
a box with a table: \vcenter{\framed{
+
    align=normal,    % justified alignment
\bTABLE[frame=off]
+
    frame=off,      % no frame
\bTR\bTD 1 \eTD\eTR
+
    width=1em]       % make box 1em wide; note that line breaking is not the same as in LaTeX (?)
\bTR\bTD 2 \eTD\eTR
+
  {1 2 3}
\bTR\bTD 3 \eTD\eTR
 
\eTABLE}}
 
 
</texcode>
 
</texcode>
  
It works on older ConTeXt distributions, here it seems to be broken:
+
This produces the following pair of results:
  
 
<context>
 
<context>
a box with a table: \vcenter{\framed{
+
a box with a table:
\bTABLE[frame=off]
+
\framed
\bTR\bTD 1 \eTD\eTR
+
  [location=middle, % vertically centered
\bTR\bTD 2 \eTD\eTR
+
    align=flushleft, % align=no (default) is a normal hbox
\bTR\bTD 3 \eTD\eTR
+
                    % align=flushleft/middle/flushright/normal is used for multiple lines
\eTABLE}}
+
    offset=5pt]     % to make the frame wider (as in LaTeX)
 +
  {1\\2\\3}
 +
 
 +
a midaligned parbox:
 +
\framed
 +
  [location=middle, % vertically centered
 +
    align=normal,    % justified alignment
 +
    frame=off,      % no frame
 +
    width=1em]      % make box 1em wide; note that line breaking is not the same as in LaTeX (?)
 +
  {1 2 3}
 
</context>
 
</context>
  
The second box <b>should</b> be possible to typeset as
+
This is simply a replacement for the vertical centering; to replace a <code>tabular</code> environment that's a proper table with more than one column, use <code>\bTABLE ... \eTABLE</code> or something similar instead of <code>1\\2\\3</code>.  See the [[Tables Overview]] for a discussion of the different methods for doing tables in ConTeXt.
<texcode>
 
\setupTABLE[frame=on]
 
\setupTABLE[c][1,2][align=lohi]
 
\bTABLE
 
\bTR\bTD
 
    a midaligned parbox:
 
\eTD\bTD[offset=0pt,width=1em]
 
    1 2 3
 
\eTD\eTR
 
\eTABLE
 
</texcode>
 
  
but it doesn't work:
+
[[Category:Basics]]
 
+
[[Category:From LaTeX]]
<context>
 
\setupTABLE[frame=on]
 
\setupTABLE[c][1,2][align=lohi]
 
\bTABLE
 
\bTR\bTD
 
    a midaligned parbox:
 
\eTD\bTD[offset=0pt,width=1em]
 
    1 2 3
 
\eTD\eTR
 
\eTABLE
 
</context>
 

Latest revision as of 13:20, 9 August 2020

LaTeX

In LaTeX, it's fairly easy to produce vertically centered boxes, using optional arguments to either the \parbox command or the tabular environment, as in this example.

\documentclass{article}
\begin{document}
a box with a table:
\fbox{\begin{tabular}[c]{l}1\\2\\3\end{tabular}}\par
a midaligned parbox: \parbox[c]{1em}{1 2 3}
\end{document}

ConTeXt

In ConTeXt, the \framed command can be used to produce similar results. To produce exactly the same output as the LaTeX commands requires the specification of rather more options, but this is mainly because the defaults are different.

a box with a table:
\framed
   [location=middle, % vertically centered
    align=flushleft, % align=no (default) is a normal hbox
                     % align=flushleft/middle/flushright/normal is used for multiple lines
    offset=5pt]      % to make the frame wider (as in LaTeX)
   {1\\2\\3}

a midaligned parbox:
\framed
   [location=middle, % vertically centered
    align=normal,    % justified alignment
    frame=off,       % no frame
    width=1em]       % make box 1em wide; note that line breaking is not the same as in LaTeX (?)
   {1 2 3}

This produces the following pair of results:

This is simply a replacement for the vertical centering; to replace a tabular environment that's a proper table with more than one column, use \bTABLE ... \eTABLE or something similar instead of 1\\2\\3. See the Tables Overview for a discussion of the different methods for doing tables in ConTeXt.