kaobook icon indicating copy to clipboard operation
kaobook copied to clipboard

sidecite only once per page

Open fredguth opened this issue 2 years ago • 7 comments

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 avatar Mar 04 '22 16:03 fredguth

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 avatar Mar 04 '22 16:03 fredguth

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.

fredguth avatar Mar 04 '22 20:03 fredguth

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.

pierrejacquet avatar Mar 07 '22 15:03 pierrejacquet

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.

fredguth avatar Mar 17 '22 15:03 fredguth

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.)

pi8027 avatar Apr 07 '22 14:04 pi8027

@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.

AlexanderZeilmann avatar Jun 03 '22 07:06 AlexanderZeilmann

@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.

fredguth avatar Jun 28 '22 03:06 fredguth