Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firstnumber=\ref*{marker} not working #330

Closed
duzy opened this issue May 29, 2022 · 5 comments
Closed

firstnumber=\ref*{marker} not working #330

duzy opened this issue May 29, 2022 · 5 comments
Labels

Comments

@duzy
Copy link

duzy commented May 29, 2022

Wish the firstnumber to use value like \ref*{line-in-a-file}:

\begin{minted}[firstnumber=\ref*{marker-from-foo-js}]{javascript}
function foo() {
}
\end{minted}

\inputmintedfile{foo.js}

foo.js:

...

function foo() {//$label{marker-from-foo-js}$
}

...
@muzimuzhi
Copy link
Contributor

muzimuzhi commented May 29, 2022

It's always recommended to provide a small but complete example so others can just copy and test, rather than code snippets only.

Several steps are needed:

  • for foo.js, use |\label{\detokenize{<label>}}| to add a label, then use minted option escapeinside=|| when inputting.
  • for firstnumber=<number>, use an expandable way to extract the (Arabic) value of a label. That's what \getrefnumber{<label>} from refcount package for. Note \ref(*) is not expandable.

Full example. I deliberately make the value of label different from 1, so it's easier to check if firstnumber=\getrefnumber{...} works.

\begin{filecontents}[noheader,force]{foo.js}
function foo() {
} |\mintlabel{marker-from-foo-js}|
\end{filecontents}

\documentclass{article}
\usepackage{minted}
\usepackage{refcount}

\newcommand\mintlabel[1]{%
  \label{\detokenize{#1}}%
}

\begin{document}
Use the value of label \verb|marker-from-foo-js| in setting \verb|firstnumber=<number>|
\begin{minted}[autogobble, firstnumber=\getrefnumber{marker-from-foo-js}, linenos]{javascript}
  function foo() {
  }
\end{minted}

Typeset \verb|foo.js| which defines label \verb|marker-from-foo-js|
\inputminted[escapeinside=||, linenos]{javascript}{foo.js}
\end{document}

image

@duzy
Copy link
Author

duzy commented May 29, 2022

Yes, firstnumber=\getrefnumber{...} works exactly as expected. Thank you!

@duzy duzy closed this as completed May 29, 2022
@duzy
Copy link
Author

duzy commented May 30, 2022

Hi, is it possible to have the similar function for highlightlines?

\inputminted[escapeinside=||, linenos, highlightlines={\getrefnumber{marker-in-foo-js}}]{javascript}{foo.js}

This doesn't work for me.

@duzy duzy reopened this May 30, 2022
@muzimuzhi
Copy link
Contributor

muzimuzhi commented May 31, 2022

Not possible until you apply some patch, for example one of the two redefinitions below. Note the original definitions live in fvextra.sty.

\makeatletter
% either expand value of `highlightlines` by \edef. This is more useful than the second one.
% before: \define@key{FV}{highlightlines}{\def\FV@HighlightLinesList{#1}}
\define@key{FV}{highlightlines}{\edef\FV@HighlightLinesList{#1}}

% or remove the \detokenize which disables expansion for #1. Is this \detokenize ever required?
\def\FV@HighlightLinesParse@Single#1{%
  % before: \expandafter\let\csname FV@HighlightLine:\detokenize{#1}\endcsname\relax
  \expandafter\let\csname FV@HighlightLine:#1\endcsname\relax}
\makeatother

@duzy duzy closed this as completed May 31, 2022
@muzimuzhi
Copy link
Contributor

Hi, is it possible to have the similar function for highlightlines?

\inputminted[escapeinside=||, linenos, highlightlines={\getrefnumber{marker-in-foo-js}}]{javascript}{foo.js}

This doesn't work for me.

Just found this was once asked and answered (by myself) two years ago (Jun 2020) on tex-sx: https://tex.stackexchange.com/a/549882 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants