Menu

#38 vertical alignment of captionbeside

KOMA-Script 4
pending
2022-11-21
2022-11-07
Oliver Natt
No

The vertical alignment of a picture and the corresponding label is wrong when a different font-size is selected for the figure caption. The following LaTeX-code demonstrates the problem:

\documentclass{scrarticle}
\setkomafont{caption}{\tiny}
\begin{document}
\begin{figure}
  \begin{captionbeside}{This is a caption.}
    \fbox{\Huge Picture}
  \end{captionbeside}
\end{figure}
\end{document}

Without the line \setkomafont{caption}{\tiny}>, both the baseline of the caption and the baseline of the word 'Picture' are perfectly aligned. However, with a different font size of the caption the two baselines are not aligned any more.

Discussion

  • Markus Kohm

    Markus Kohm - 2022-11-07
    • labels: --> scrartcl, scrreprt, scrbook
    • Milestone: KOMA-Script 3.38 --> KOMA-Script 3.39
     
  • Markus Kohm

    Markus Kohm - 2022-11-07

    Milestone changed, because I cannot do any changes to already released KOMA-Script versions. So adding tickets to already closed milestone makes no sense. Please always use the default milestone.

     
  • Markus Kohm

    Markus Kohm - 2022-11-09

    Yes, I see. The problem is the alignment of boxes using boxes. The inner box of the caption uses the font size of element caption. The inner box of the captionbeside content uses the font size of the captionbeside content. But the outer boxes both use the font size of figure environment. And because the inner boxes also use \ht\strutbox (for top alignment) and \dp\strutbox (for bottom alignment) to adapt the alignment, this cannot work correctly in this case.

    However, a change will be a little bit complicated. I also have to decide, which changes to already existing documents are tolerable and which should be avoided. I've to do some experiments with different cases to do a decision. And maybe the change should be done, depending on compatibility settings. So maybe I'll not change this until KOMA-Script 4.

    Thank you the report.

     
  • Markus Kohm

    Markus Kohm - 2022-11-09
    • labels: scrartcl, scrreprt, scrbook --> scrartcl, scrreprt, scrbook, bug
    • assigned_to: Markus Kohm
     
  • Oliver Natt

    Oliver Natt - 2022-11-09

    Thank you very much for the explanation. I understand, that doing changes influencing existing documents is not a good option. That would have a negative impact on me, too, because I have a lot of documents where I tackled the problem by using \raisebox on the figure content with an experimentally determined value for the vertical shift.

    However, for new documents, I would like to do it better. Do you have a proposal for a workaround? Based on your explanation, I came to the conclusion that setting the same font for the captionbeside content and the figure environment should fix the problem. This would make no difference for my applications since the figure content is almost always just an \includegraphics command. However, the follwoing code results in a vertical misalignment, too:

    \documentclass{scrarticle}
    \setkomafont{caption}{\tiny}
    \begin{document}
    \begin{figure}
      \tiny
      \begin{captionbeside}{This is a caption.}
        Picture
      \end{captionbeside}
    \end{figure}
    \end{document}
    
     
    • Markus Kohm

      Markus Kohm - 2022-11-09

      I think,

      \documentclass{scrartcl}
      \setkomafont{caption}{\tiny}
      \begin{document}
      \begin{figure}
        \let\xnormalsize\normalsize% optional
        \let\normalsize\tiny\normalsize
        \begin{captionbeside}{This is a caption.}
          \let\normalsize\xnormalsize\normalsize% optional
          Picture
        \end{captionbeside}
      \end{figure}
      \end{document}
      

      would work. And cross your fingers, it should still work, when I'll fix the issue. Unfortunately you cannot use LaTeX's new generic hooks to do this modification automatically.

      BTW: Because scrarticle is officially not the same as scrartcl, it could be, that with KOMA-Script 4 this name will be used for the incompatible successor of scrartcl. So if compatibility is a must, you should still use scrartcl.

       

      Last edit: Markus Kohm 2022-11-10
  • Markus Kohm

    Markus Kohm - 2022-11-10

    Only to have a more complete test file:

    \documentclass{scrartcl}
    \makeatletter\def\fps@figure{h!}\let\fps@table\fps@figure\makeatother
    \begin{document}
    \minisec{Broken}
    \begin{figure}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \setkomafont{caption}{\tiny}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \setkomafont{caption}{\huge}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \begin{captionbeside}{This is a caption.}
        \huge
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \clearpage
    \minisec{Broken}
    \KOMAoptions{captions=topbeside}
    \begin{table}
      \begin{captionbeside}{This is a caption.}
        \begin{tabular}[t]{l}
          \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
          \rule{5cm}{\ht\strutbox}
        \end{tabular}
      \end{captionbeside}
    \end{table}
    \begin{table}
      \setkomafont{caption}{\tiny}
      \begin{captionbeside}{This is a caption.}
        \begin{tabular}[t]{l}
          \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
          \rule{5cm}{\ht\strutbox}
        \end{tabular}
      \end{captionbeside}
    \end{table}
    \begin{table}
      \setkomafont{caption}{\huge}
      \begin{captionbeside}{This is a caption.}
        \begin{tabular}[t]{l}
          \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
          \rule{5cm}{\ht\strutbox}
        \end{tabular}
      \end{captionbeside}
    \end{table}
    \begin{table}
      \begin{captionbeside}{This is a caption.}
        \huge
        \begin{tabular}[t]{l}
          \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
          \rule{5cm}{\ht\strutbox}
        \end{tabular}
      \end{captionbeside}
    \end{table}
    \clearpage
    \minisec{Works}
    \KOMAoptions{captions=centeredbeside}
    \begin{figure}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \setkomafont{caption}{\tiny}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \setkomafont{caption}{\huge}
      \begin{captionbeside}{This is a caption.}
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \begin{figure}
      \begin{captionbeside}{This is a caption.}
        \huge
        \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
      \end{captionbeside}
    \end{figure}
    \end{document}
    
     
    • Markus Kohm

      Markus Kohm - 2022-11-10

      And here is a first fix for it:

      \documentclass{scrartcl}
      \makeatletter\def\fps@figure{h!}\let\fps@table\fps@figure\makeatother
      
      % Following code fixes the broken vertical alignment of captionbeside.
      \makeatletter
      \long\def\c@pti@nb@sid@[#1]#2{%
        \def\scap@caption{%
          \abovecaptionskip\z@
          \belowcaptionskip\z@
          \if t\scap@valign
            {%
              \usekomafont{caption}{%
                \global\@tempdima\ht\strutbox
              }%
            }%
            \setlength{\abovecaptionskip}{-\@tempdima}%
          \fi
          \if b\scap@valign  
            {%
              \usekomafont{caption}{%
                \global\@tempdima\dp\strutbox
              }%
            }%
            \setlength{\belowcaptionskip}{\dimexpr\dp\strutbox-2\@tempdima\relax}%
          \fi
          \captionbelow[{#1}]{#2}%
        }%
        \begin{lrbox}{\c@pti@nb@sid@b@x}%
      }
      \makeatother
      %\usepackage{caption}% With this, the current patch would not work.
      \begin{document}
      \minisec{No longer broken}
      \begin{figure}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \setkomafont{caption}{\tiny}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \setkomafont{caption}{\huge}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \huge
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \clearpage
      \minisec{No longer broken}
      \KOMAoptions{captions=topbeside}
      \begin{table}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \begin{tabular}[t]{l}
            \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
            \rule{5cm}{\ht\strutbox}
          \end{tabular}
        \end{captionbeside}
      \end{table}
      \begin{table}
        \setkomafont{caption}{\tiny}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \begin{tabular}[t]{l}
            \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
            \rule{5cm}{\ht\strutbox}
          \end{tabular}
        \end{captionbeside}
      \end{table}
      \begin{table}
        \setkomafont{caption}{\huge}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \begin{tabular}[t]{l}
            \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
            \rule{5cm}{\ht\strutbox}
          \end{tabular}
        \end{captionbeside}
      \end{table}
      \begin{table}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \huge
          \begin{tabular}[t]{l}
            \rule{5cm}{\ht\strutbox}\makebox[0pt][l]{\rule{10cm}{.4pt}}\\
            \rule{5cm}{\ht\strutbox}
          \end{tabular}
        \end{captionbeside}
      \end{table}
      \clearpage
      \minisec{Works}
      \KOMAoptions{captions=centeredbeside}
      \begin{figure}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \setkomafont{caption}{\tiny}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \setkomafont{caption}{\huge}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \begin{figure}
        \begin{captionbeside}{This\rule[-\dp\strutbox]{1pt}{\baselineskip} is a caption.}
          \huge
          \rule{5cm}{2cm}\makebox[0pt][l]{\rule{10cm}{.4pt}}
        \end{captionbeside}
      \end{figure}
      \end{document}
      

      Note: The additional \rule in the caption is there only to illustrate the real height and depth of the caption line. I've also tested with several lines of caption text. And there it also seems to work.

      However, I still do not know, how I will add it, because the change will change already existing documents.

      And another problem is, that this fix does not work, if package caption is loaded and currently I do not know, whether this is an issue of KOMA-Script or of caption.

      See also my notes to [r3983].

       

      Related

      Commit: [r3983]
      Issues: #1


      Last edit: Markus Kohm 2022-11-10
  • Markus Kohm

    Markus Kohm - 2022-11-10
    • status: open --> pending
     
  • Oliver Natt

    Oliver Natt - 2022-11-12

    Thanks a lot for the fix. I can use this as a good workaround for my own documents, because I do not use the package caption. However, I think it would be highly desirable to make the patch compatible with the package caption since it is used by many people. Unfortunately, my LaTeX-knowledge is insufficient to help you in figuring out the cause of this problem.

    Regarding the question how to add the patch to KOMA-Script: Maybe you could add a KOMAoption (baselinealignedbeside or something similar).

     
  • Markus Kohm

    Markus Kohm - 2022-11-21
    • Milestone: KOMA-Script 3.39 --> KOMA-Script 4
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.