fmarotta / kaobook

A LaTeX class for books, reports or theses based on https://github.com/kenohori/thesis and https://github.com/Tufte-LaTeX/tufte-latex.
https://github.com/fmarotta/kaobook
LaTeX Project Public License v1.3c
858 stars 184 forks source link

sidecite only once per page #216

Open fredguth opened 2 years ago

fredguth commented 2 years ago

Summary

Currently, sidecite appears to be adding citation to the margin everytime you call sidecite.

Details

When you \sidecite{key} more than once \sidecite{key} in the same page. There should be only one citation in the margin.

See: https://tex.stackexchange.com/q/444827/157312

I am trying to fix this, if successful I will PR a change. But I am still a beginner with kaobook code.

fredguth commented 2 years ago

Besides, documentation says you cannot change the position of citation in the margin, although I believe you already created this feature in the code.

fredguth commented 2 years ago

Changing kaobiblio.sty I was able to mark sidenotes as new (N) or old (O), but when I tried to prevent \formatmargincitation{...} of running, there is an error.

This works:

\NewDocumentCommand{\registerkey}{}{%
  \ifcsundef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}
    {\csnumgdef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}{0}}
    {}%
  \csnumgdef{cbx@instcount@last@\the\c@refsection @\thefield{entrykey}}{%
    \csuse{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}}%
  \csnumgdef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}{\value{instcount}}}

\def\iflastciteonsamepage{
  \ifsamepage
    {\number\csuse{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}}
    {\number\csuse{cbx@instcount@last@\the\c@refsection @\thefield{entrykey}}}
}
\NewDocumentCommand{\sidecite}{o o o m}{%
    \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
        \marginnote[#1]{##1}%
    }%
    \DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
    }{%
        \registerkey
        \iflastciteonsamepage
        {O)}
        {N)}
        \formatmargincitation{\thefield{entrykey}}
    }{%
        \\% separator between multiple citations
    }{%
    }%
    % With this we print the marker in the text and add the item to the bibliography at the end
    \IfNoValueOrEmptyTF{#2}%
    {\def\@tempa{\cite{#4}\kaobiblio@sidecite{#4}}}%
    {\IfNoValueOrEmptyTF{#3}%
        {\IfNoValueTF{#3}%
            {\def\@tempa{\cite[#2]{#4}\kaobiblio@sidecite{#4}}}%
            {\def\@tempa{\cite[#2][]{#4}\kaobiblio@sidecite{#4}}}% postnote is empty, so pass empty postnote
        }%
        {\def\@tempa{\cite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
    }%
    \ifkaobiblio@addspace%
        \unskip~\@tempa%
    \else%
        \@tempa%
    \fi%
}

This does not work:

\NewDocumentCommand{\registerkey}{}{%
  \ifcsundef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}
    {\csnumgdef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}{0}}
    {}%
  \csnumgdef{cbx@instcount@last@\the\c@refsection @\thefield{entrykey}}{%
    \csuse{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}}%
  \csnumgdef{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}{\value{instcount}}}

\def\iflastciteonsamepage{
  \ifsamepage
    {\number\csuse{cbx@instcount@curr@\the\c@refsection @\thefield{entrykey}}}
    {\number\csuse{cbx@instcount@last@\the\c@refsection @\thefield{entrykey}}}
}
\NewDocumentCommand{\sidecite}{o o o m}{%
    \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
        \marginnote[#1]{##1}%
    }%
    \DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
    }{%
        \registerkey
        \iflastciteonsamepage
        {%nothing here%}
        {\formatmargincitation{\thefield{entrykey}}}

    }{%
        \\% separator between multiple citations
    }{%
    }%
    % With this we print the marker in the text and add the item to the bibliography at the end
    \IfNoValueOrEmptyTF{#2}%
    {\def\@tempa{\cite{#4}\kaobiblio@sidecite{#4}}}%
    {\IfNoValueOrEmptyTF{#3}%
        {\IfNoValueTF{#3}%
            {\def\@tempa{\cite[#2]{#4}\kaobiblio@sidecite{#4}}}%
            {\def\@tempa{\cite[#2][]{#4}\kaobiblio@sidecite{#4}}}% postnote is empty, so pass empty postnote
        }%
        {\def\@tempa{\cite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
    }%
    \ifkaobiblio@addspace%
        \unskip~\@tempa%
    \else%
        \@tempa%
    \fi%
}

First I thought the problem was the '\' without nothing before, but it is more than that.

Hope someone can help.

pierrejacquet commented 2 years ago

Looking for this functionnality too. It would be nice if this modification could also integrate the option to sidecite a reference once per document (sidecite only the first time). So like a switch between "each time/ once per page / once per document". I have no idea if it's feasible since I have little knowledge in LaTeX prog.

fredguth commented 2 years ago

This issue seems related to #217 and #220.

@fmarotta, I guess you are too busy to make these changes. Could you take a glance at least to give us some hints on how could we try to fix this issue? I would gladly help if I could. But, at this point, I have no idea how.

pi8027 commented 2 years ago

I'm looking for this feature too. But I'd like to do that selectively for each page (for pages with fewer citations, I prefer not doing that), and to control their position since I use \sidenote a lot. (I just wanted to express my needs, but I have no idea how to implement that.)

AlexanderZeilmann commented 2 years ago

@fredguth may I ask what error you encountered with your code? For me it seems to work just fine.

The only change I made was to convert

\iflastciteonsamepage
{%nothing here%}
{\formatmargincitation{\thefield{entrykey}}}

to

\iflastciteonsamepage
{}
{\formatmargincitation{\thefield{entrykey}}}

such that the first closing bracket is not commented out.

The only problem is that the horizontal alignment of the first line of the margin citation is slightly misaligned. This problem is probably the same as #220.

fredguth commented 2 years ago

@AlexanderZeilmann I created a PR for this feature. Although, it is breaking another feature.... /sidecite{key1, key2} does not work anymore, so I don't know if it will be merged.

On the other matter... Indeed, I tried addspace=false and addspace=true and it makes no difference.