Generallineno number caption w/ honor '\linenumbers'

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

lineno number caption w/ honor '\linenumbers'

Post by gallioleo »

Hello,

i like to use the lineno package to numbering my document. For numbering the caption I use the fix from 'https://tex.stackexchange.com/questions/579040/figure-caption-line-numbers-with-achemso-package?rq=1'

\usepackage{lineno}
\DeclareCaptionFont{linenumbers}{\internallinenumbers}
\captionsetup[figure]{font+=linenumbers} % use \internallinenumbers inside figure caption


This works also well, but to enable the line numbering i use the switch '\linenumbers'. With this fix the caption are always numbered w/ and w/o the line numbering enabling.

Does anyone has an idea, how i can number the caption with honoring the '\linenumbers' switch?

Thanks in advance.

Best Regards.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

lineno number caption w/ honor '\linenumbers'

Post by Ijon Tichy »

Without Infominimal working example I can only speculate. Maybe:

Code: Select all

\DeclareCaptionFont{linenumbers}{\ifLineNumbers\internallinenumbers\fi}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

lineno number caption w/ honor '\linenumbers'

Post by gallioleo »

Here the MWE:

Code: Select all

\documentclass[
        pagesize=pdftex,
        12pt,
        DIV=14,
        paper=a4,
        final
]{scrreprt}


\usepackage[english]{babel}
\usepackage[hang,footnotesize,bf]{caption}
\usepackage[left, pagewise, edtable]{lineno}


% enable line numbers for caption
\DeclareCaptionFont{linenumbers}{\ifLineNumbers\internallinenumbers\fi}
\captionsetup[figure]{font+=linenumbers}


% Start Document
\begin{document}

\linenumbers

My Example text before figure

\begin{figure}[h]
	\caption{MyCaption}
\end{figure}

and after figure

\end{document}
The Compile result looks like follow:
Image
Attachments
Zwischenablage01.png
Zwischenablage01.png (17.84 KiB) Viewed 10494 times
Last edited by Stefan Kottwitz on Fri Nov 19, 2021 11:54 am, edited 1 time in total.
Reason: Code tags fixed
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

lineno number caption w/ honor '\linenumbers'

Post by Ijon Tichy »

Please use code tags for code. MWE tags are to be used to add a Infominimal working example link only!

Code: Select all

\documentclass[
pagesize=pdftex,% Nonsense, because option pagesize with detection of backend
                % is te default!
12pt,
DIV=14,
paper=a4,% This is also the default.
final
]{scrreprt}


\usepackage[english]{babel}% It would be better to set the language as global option.
\usepackage[hang,footnotesize,bf]{caption}
\usepackage[left, pagewise, edtable]{lineno}

% enable line numbers for caption
\DeclareCaptionFont{linenumbers}{\internallinenumbers}
\AddToHook{env/figure/begin}{% Needs LaTeX 2020/10/01 or newer!
  \ifLineNumbers\captionsetup{font+=linenumbers}\fi
}

% Start Document
\begin{document}

My Example text before figure and line numbering

\begin{figure}[ht]% single option h also does work like ht
\caption{MyCaption}
\end{figure}

\linenumbers

My Example text before figure

\begin{figure}[ht]% single option h also does work like ht
\caption{MyCaption}
\end{figure}

and after figure

\end{document}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

lineno number caption w/ honor '\linenumbers'

Post by gallioleo »

Thanks for the code - great work! The other comment I'll honot also.
One hint the figure caption line has the same number like the line before the caption. This happends only if i use pagewise numbering. Do you have therefore a suggestion?

Thanks a lot again!
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

lineno number caption w/ honor '\linenumbers'

Post by Ijon Tichy »

The problem is, that figure is a floating environment. The location of floats is not known before the output routine. So it I think it could be very hard to change this. I would not expect this to work. However, you could adjust the line number using \resetlinenumber, but only if you know, where the figure is placed in the output.

I'm even surprised, that line numbering in floats does work anyway.

So, no, I cannot help in this.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

lineno number caption w/ honor '\linenumbers'

Post by gallioleo »

No problem, i switch to continous numbering and then did does not happen.
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

lineno number caption w/ honor '\linenumbers'

Post by gallioleo »

Hi again, for the tabular environment i like to use the same hook, but at the moment i fail to get it to work. Please take a look again:

Code: Select all

\documentclass[
        12pt,
        DIV=14,
        main=english,
        final
]{scrreprt}


\usepackage{babel}
\usepackage[hang,footnotesize,bf]{caption}
\usepackage[left, pagewise, edtable]{lineno}


% table line number moved to table and not on page margin
%   https://tex.stackexchange.com/questions/85217/linenumbers-with-lineno-edtable-breaks-inside-threeparttable-because-of-captio
\makeatletter
\newenvironment{etabular}
  {\resetlinenumber[1]\let\@centercr\relax\edtable{tabular}}
  {\endedtable}
\newenvironment{etabular*}
  {\resetlinenumber[1]\let\@centercr\relax\edtable{tabular*}}
  {\endedtable}
\makeatother


% enable line numbers for tabular
%   https://www1.pub.informatik.uni-wuerzburg.de/eurocg2020/data/uploads/eurocg20-submission.cls
\let\linenotabular\tabular%
\let\endlinenotabular\endtabular%
\renewenvironment{tabular}{\begin{edtable}{linenotabular}}{\end{edtable}}%


% Start Document
\begin{document}

\linenumbers

My Example text before table

\begin{table}[h]
    \centering
    \begin{tabular}{|l|}        \hline
        row1                \\ \hline
        row2                \\ \hline
    \end{tabular}
\end{table}

and after.

\end{document}

Post Reply