5

I am using the very nice package tabularray and I want to put the caption of the table on the side margin (like Tufte-Style), but I did not find a way to do it. In the documentation (4.2.3 of the manual) I just found a way to customize the font and other options, with the commands:

\DefTblrTemplate{caption-tag}{default}{Table\hspace{0.25em}\thetable}
\DefTblrTemplate{caption-sep}{default}{:\enskip}
\DefTblrTemplate{caption-text}{default}{\InsertTblrText{caption}}

But this does not affect the position of the caption.

Here a very MWE:

\documentclass[12pt, mpinclude]{scrbook}

\usepackage{tabularray}

\begin{document}

\DefTblrTemplate{caption-tag}{default}{Tabela\hspace{0.25em}\thetable}
\DefTblrTemplate{caption-sep}{default}{-\enskip}
\DefTblrTemplate{caption-text}{default}{\InsertTblrText{caption}}
\begin{longtblr}[caption={one table}]{colspec = {X[c]X[c]X[c]}}
1 &  2& 3 &\\
4 & 5& 6\\
7 & 8 & 9 \\
\end{longtblr}
\end{document}

EDIT: I mentioned the Tufte-Style just because I am trying to reproduce this style in a document and it uses all captions in the margin, but I am not using this specific class.

2
  • 1
    I think you should clarify if you want a longtblr solution. If so, I think you have to redefine some commands of the package, which is unfortunately not that easy. Maybe contact the package author for further help.
    – marv
    Dec 20, 2021 at 7:49
  • Yes, I want a longtblr solution! I redefined the commands and, for me, worked. Dec 20, 2021 at 13:50

2 Answers 2

2

A simple solution is to remove the standard caption entirely, and the use \captionof in a \marginpar. \captionof is a standard command in soma-script, otherwise you would have to use a package like caption.

\documentclass[12pt, mpinclude]{scrbook}

\usepackage{tabularray}

\setcapindent{0pt}

\begin{document}

\DefTblrTemplate{caption-tag}{default}{}
\DefTblrTemplate{caption-sep}{default}{}
\DefTblrTemplate{caption-text}{default}{}
\renewcommand{\tablename}{Tabela}

\marginpar{\captionof{table}{\null\\one table}}
\begin{longtblr}[caption={one table}]{colspec = {X[c]X[c]X[c]}}
1 &  2& 3 &\\
4 & 5& 6\\
7 & 8 & 9 \\
\end{longtblr}
\end{document}

enter image description here

1
  • Thank you for your comment! I was looking to a more automatic solution, with the internal commands of the tabularray package, fortunately I found it! Dec 20, 2021 at 13:48
2

I found a solution! You need to redefine/create a template command. I used the code bellow to redefine the default template.

\DefTblrTemplate { caption } { normal }
{
\hbox_set:Nn \l__tblr_caption_box
{
\UseTblrTemplate { caption-tag } { default }
\UseTblrTemplate { caption-sep } { default }
\UseTblrTemplate { caption-text } { default }
}
\dim_compare:nNnTF { \box_wd:N \l__tblr_caption_box } > { \hsize }
{
\UseTblrAlign { caption }
\UseTblrIndent { caption }
\hbox_set:Nn \l__tblr_caption_left_box
{
\UseTblrTemplate { caption-tag } { default }
\UseTblrTemplate { caption-sep } { default }
}
\hangindent = \box_wd:N \l__tblr_caption_left_box
\hangafter = 1
\UseTblrHang { caption }
\leavevmode
\hbox_unpack:N \l__tblr_caption_box
\par
}
{
\centering
\makebox [\hsize] [c] { \box_use:N \l__tblr_caption_box }
\par
}
}

I redefined the default style to this:

 \DefTblrTemplate { caption } { default }
{
    \hbox_set:Nn \l__tblr_caption_box
    {
        \UseTblrTemplate { caption-tag } { default }
        \UseTblrTemplate { caption-sep } { default }
        \UseTblrTemplate { caption-text } { default }
    }
    \dim_compare:nNnTF { \box_wd:N \l__tblr_caption_box } > { \hsize }
        {
            \UseTblrAlign { caption }
            \UseTblrIndent { caption }
            \hbox_set:Nn \l__tblr_caption_left_box
            {
                \UseTblrTemplate { caption-tag } { default }
                \UseTblrTemplate { caption-sep } { default }
            }
            \hangindent = \box_wd:N \l__tblr_caption_left_box
            \hangafter = 1
            \UseTblrHang { caption }
            \leavevmode
            \hbox_unpack:N \l__tblr_caption_box
            \par
        }
        {
            \marginnote { \box_use:N \l__tblr_caption_box }
            \par
        }
}

Here I changed these lines of placement:

\centering
    \makebox [\hsize] [c] { \box_use:N \l__tblr_caption_box }
    \par

To these lines:

\marginnote { \box_use:N \l__tblr_caption_box }
            \par

Now the caption is in the margin. Unfortunately, the \marginpar command does not work, if you try to use it, you will get the error: ! LaTeX Error: Not in outer par mode. So, you need to use the package marginnote to get the correct command.

The complete code:

\documentclass[12pt, mpinclude]{scrbook}

\usepackage{tabularray}
\usepackage{marginnote}
\begin{document}

\ExplSyntaxOn
\DefTblrTemplate { caption } { default }
{
    \hbox_set:Nn \l__tblr_caption_box
    {
        \UseTblrTemplate { caption-tag } { default }
        \UseTblrTemplate { caption-sep } { default }
        \UseTblrTemplate { caption-text } { default }
    }
    \dim_compare:nNnTF { \box_wd:N \l__tblr_caption_box } > { \hsize }
        {
            \UseTblrAlign { caption }
            \UseTblrIndent { caption }
            \hbox_set:Nn \l__tblr_caption_left_box
            {
                \UseTblrTemplate { caption-tag } { default }
                \UseTblrTemplate { caption-sep } { default }
            }
            \hangindent = \box_wd:N \l__tblr_caption_left_box
            \hangafter = 1
            \UseTblrHang { caption }
            \leavevmode
            \hbox_unpack:N \l__tblr_caption_box
            \par
        }
        {
            \marginnote { \box_use:N \l__tblr_caption_box }
            \par
        }
}
\ExplSyntaxOff

\DefTblrTemplate{caption-tag}{default}{Tabela\hspace{0.25em}\thetable}
\DefTblrTemplate{caption-sep}{default}{-\enskip}
\DefTblrTemplate{caption-text}{default}{\InsertTblrText{caption}}
\begin{longtblr}[caption={one table}]{colspec = {X[c]X[c]X[c]}}
    1 &  2& 3 &\\
    4 & 5& 6\\
    7 & 8 & 9 \\
\end{longtblr}
\end{document}

The output:

enter image description here

4
  • 3
    (+1) You may consider adding an image of the output. It helps people to quickly find what they need.
    – Imran
    Dec 20, 2021 at 13:56
  • (+1) for try to solve problem in tabularray world, however in question you mentioned tufte-style of document layout, so I expect that you use this documentclass :-).
    – Zarko
    Dec 20, 2021 at 14:59
  • 1
    @Imran I added the output! Thanks for the suggestion. Dec 20, 2021 at 17:13
  • @Zarko I added a edit about it, but I did not tested it in tufte-style. Actually I am using scrbook. Dec 20, 2021 at 17:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .