1

Edit: I would like to know what the best practice is for creating a method to edit the desired optional arguments of all instances of an environment from one location.

Motivation: So I'm using minted and am not set on the general styling I want from my code. So I decide to wrap the environment (doesn't work; "File ended while scanning use of \FancyVerbGetLine."), then I decided to try and use a variable (doesn't work; "Package keyval: "breaklines, showspaces" undefined.").

Minted has a nice command \setminted[⟨language⟩]{⟨key=value,...⟩} which dose what I want but for next time I need to I would like to know the correct approach to take for this kind off issue. Maybe something to do with \renewenvironment?

\documentclass{article}

\usepackage{minted} \usemintedstyle{vs}

\newenvironment{code}{\begin{minted}[frame=lines]{python3}}{\end{minted}} \def\pythonarguments{breaklines, showspaces}

\begin{document}

% \begin{minted}[\pythonarguments]{python3} % def f_circle(T, R, x=0, y=0): % X = x + Rnp.cos(T) % Y = y + Rnp.sin(T)

% return X, Y % \end{minted}

% \begin{code} % def f_circle(T, R, x=0, y=0): % X = x + Rnp.cos(T) % Y = y + Rnp.sin(T)

% return X, Y % \end{code}

\end{document}

GBR6000
  • 56
  • 2
    I don't understand. Isn't the entire point of \setminted that you do that in the preamble, and then next time you can come back and change the settings there? Why are you trying to do it elsewhere when minted has provided a way to do it? – Teepeemm Sep 15 '23 at 15:05
  • I'm trying to ask a slightly more general question about the best practices for any random environment that I may come across that doesn't have minted's ability to nicely define this at the start. – GBR6000 Sep 15 '23 at 15:11
  • If it is "in general" rather than specific to minted or phython3, you can always define a macro that contains the desired global settings, then place that macro as an option. If the macro contains other macros within it, you might need to use \edef or \xdef rather than \def or gdef, but not necessarily. – rallg Sep 15 '23 at 15:38
  • @rallg This only works, if the optional argument is expanded at least once before it is split into single options. So I doubt, that you can always do this. – cabohah Sep 15 '23 at 15:44
  • @GBR6000 verbatim like or source code environments are somehow special. Because of this \newenvironment does not always work. For example, listings has its own command to define new environments. Because of this, you should always search in the corresponding manual. – cabohah Sep 15 '23 at 15:48
  • @cabohah is correct, regarding my earlier comment. – rallg Sep 15 '23 at 15:52
  • Why not use \newminted? Documented on page 28 of manual and example here – mbert Sep 15 '23 at 16:45

1 Answers1

2

The attempt with

\def\pythonarguments{breaklines, showspaces}

\begin{minted}[\pythonarguments]{python3}

isn't going to work, because the argument is not expanded looking for key-value options.

You might do

\documentclass{article}

\usepackage{minted} \usemintedstyle{vs}

\newenvironment{code}{\VerbatimEnvironment\begin{minted}[frame=lines]{python3}}{\end{minted}}

\begin{document}

\begin{code} def f_circle(T, R, x=0, y=0): X = x + Rnp.cos(T) Y = y + Rnp.sin(T)

return X, Y

\end{code}

\end{document}

enter image description here

But the manual tells you about \newminted, by which you can also specify further options with the * version.

\documentclass{article}

\usepackage{minted} \usemintedstyle{vs}

\newminted[code]{python3}{frame=lines}

\begin{document}

\begin{code} def f_circle(T, R, x=0, y=0): X = x + Rnp.cos(T) Y = y + Rnp.sin(T)

return X, Y

\end{code}

\end{document}

enter image description here

egreg
  • 1,121,712