다음 new명령을 수정하는 방법은 무엇입니까?

다음 new명령을 수정하는 방법은 무엇입니까?

다음과 같이 새 명령을 선언합니다.

\newcommand{\code}[2]{
\begin{listing}[ht]
\begin{minted}
[
framesep=2mm,
baselinestretch=1.2,
bgcolor=blue!30!white,
fontsize=\footnotesize,
]
{#1}
#2
\end{minted}
\end{listing}%$
}

나는 다음과 같이 사용하고 싶습니다 :

\code{bash}{
sudo apt-get install golang python3-dev python-dev libcupti-dev libjpeg-turbo8-dev \
make tmux htop chromium-browser git cmake zlib1g-dev libjpeg-dev  \
xvfb xorg-dev python-opengl libboost-all-dean v libsdl2-dev swig
}

불행하게도 오류가 발생하고 있습니다.

이 문제를 어떻게 수정해야 합니까?

답변1

환경을 사용하면 훨씬 간단합니다.

\documentclass{article}
\usepackage{minted}

\newenvironment{code}[2][]
 {%
  \VerbatimEnvironment
  \begin{listing}[htp]
  \begin{minted}[
    framesep=2mm,
    baselinestretch=1.2,
    bgcolor=blue!30!white,
    fontsize=\footnotesize,
    #1
  ]{#2}%
 }
 {\end{minted}\end{listing}}

\begin{document}

\begin{code}[breaklines]{bash}
sudo apt-get install golang python3-dev python-dev libcupti-dev libjpeg-turbo8-dev \
make tmux htop chromium-browser git cmake zlib1g-dev libjpeg-dev  \
xvfb xorg-dev python-opengl libboost-all-dean v libsdl2-dev swig
\end{code}

\begin{listing}[htp]
\begin{minted}[
  framesep=2mm,
  baselinestretch=1.2,
  bgcolor=blue!30!white,
  fontsize=\footnotesize,
  ]{bash}
sudo apt-get install golang python3-dev python-dev libcupti-dev libjpeg-turbo8-dev \
make tmux htop chromium-browser git cmake zlib1g-dev libjpeg-dev  \
xvfb xorg-dev python-opengl libboost-all-dean v libsdl2-dev swig
\end{minted}
\end{listing}

\end{document}

minted첫 번째 예에서는 환경 에 옵션을 추가하는 방법을 보여주었습니다 .

여기에 이미지 설명을 입력하세요

답변2

내용을 축어적으로 읽은 다음 \scantokens다시 토큰화하기 위해 전달하는 명령을 만들 수 있습니다.

\UDcollectverbarg다음 구문을 사용하여 매크로를 제공할 수 있습니다 .

\UDcollectverbarg{⟨^^M-replacement⟩}{⟨Mandatory 1⟩}{⟨Mandatory 2⟩}⟨verbatimized argument⟩

결과는 다음과 같습니다.

⟨Mandatory 1⟩{⟨Mandatory 2⟩{⟨verbatimized argument⟩}}

^^M, 줄의 끝을 나타내는 각 문자는 token-sequence 로 대체됩니다 ⟨^^M-replacement⟩.

필수 인수는 필수입니다. 여러 토큰으로 구성된 경우 catcode-1/2-문자 쌍/중괄호에 중첩되어야 합니다.
읽기 및 토큰화가 필요한 경우 변경되지 않은 카테고리 코드 체계에 따라 수행됩니다.
축어-Arg도 필수입니다. 이는 축어적 카테고리 코드 체계에 따라 읽고 토큰화됩니다. 첫 번째 문자가 중괄호이면 인수가 중괄호 안에 중첩되어 있다고 "가정"됩니다. 그렇지 않으면 해당 인수의 끝은 의 인수처럼 첫 번째 문자로 구분되는 것으로 가정됩니다 \verb.
빈 줄은 무시되지 않습니다.

\UDcollectverbarg이 구문을 사용하면 의 첫 번째 필수 인수 내에 호출을 중첩하여 두 번째 필수 인수 내 에서 축어화된 인수를 수집할 수 있기 때문에 이 구문을 선택했습니다 \UDcollectverbarg.

예:

\UDcollectverbarg{⟨^^M-replacement⟩}%
                 {\UDcollectverbarg{⟨^^M-replacement⟩}{\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}}}%  <- Mandatory 1
                 {⟨actionB⟩}%                     <- Mandatory 2
                 ⟨verbatimized argument 1⟩⟨verbatimized argument 2⟩⟨verbatimized argument 3⟩

수익률:

\UDcollectverbarg{⟨^^M-replacement⟩}{\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}}% <- Mandatory 1
                 {⟨actionB⟩{⟨verbatimized argument 1⟩}}%        <- Mandatory 2
                 ⟨verbatimized argument 2⟩⟨verbatimized argument 3⟩

수익률:

\UDcollectverbarg{⟨^^M-replacement⟩}{⟨actionA⟩}% <- Mandatory 1
                 {⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}}% <- Mandatory 2
                 ⟨verbatimized argument 3⟩

수익률:

⟨actionA⟩{⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}}

가정 ⟨actionA⟩= \@firstofone:

\@firstofone{⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}}

수익률:

⟨actionB⟩{⟨verbatimized argument 1⟩}{⟨verbatimized argument 2⟩}{⟨verbatimized argument 3⟩}
%% Copyright (C) 2007 - 2019 by Ulrich Diez ([email protected])
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public Licence (LPPL), either
%% version 1.3 of this license or (at your option) any later
%% version. (The latest version of this license is in:
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 1999/12/01 or later.)
%% The author of this work is Ulrich Diez.
%% This work has the LPPL maintenance status 'not maintained'.
%% Usage of any/every component of this work is at your own risk.
%% There is no warranty - neither for probably included
%% documentation nor for any other part/component of this work.
%% If something breaks, you usually may keep the pieces.

\errorcontextlines=10000

%%<-------------------- code for \UDcollectverbarg -------------------->
\makeatletter
%%......................................................................
%% Check whether argument is empty:
%%......................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\@secondoftwo\string{\expandafter
  \@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
  \@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
  \@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}%
%%......................................................................
\begingroup
\catcode`\^^M=12 %
\@firstofone{%
  \endgroup%
  \newcommand\UDEndlreplace[2]{\romannumeral0\@UDEndlreplace{#2}#1^^M\relax{}}%
  \newcommand*\@UDEndlreplace{}%
  \long\def\@UDEndlreplace#1#2^^M#3\relax#4#5{%
    \UD@CheckWhetherNull{#3}%
    { #5{#4#2}}{\@UDEndlreplace{#1}#3\relax{#4#2#1}{#5}}%
  }%
}%
\newcommand\UDcollectverbarg[3]{%
  \@bsphack
  \begingroup
  \let\do\@makeother % <- this and the next line switch to
  \dospecials        %    verbatim-category-code-régime.
  \catcode`\{=1      % <- give opening curly brace the usual catcode so a 
                     %    curly-brace-balanced argument can be gathered in
                     %    case of the first thing of the verbatimized-argument 
                     %    being a curly opening brace.
  \catcode`\ =10     % <- give space and horizontal tab the usual catcode so
  \catcode`\^^I=10   %    \UD@collectverbarg cannot catch a space or horizontal
                     %    tab as its 4th undelimited argument.
                     %    (Its 4th undelimited argument denotes the verbatim-
                     %     syntax-delimiter in case of not gathering a
                     %     curly-brace-nested argument.)
  \kernel@ifnextchar\bgroup
  {% seems a curly-brace-nested argument is to be caught:
    \catcode`\}=2    % <- give closing curly brace the usual catcode also.
    \UD@collectverbarg{#1}{#2}{#3}{}%
  }{% seems an argument with verbatim-syntax-delimiter is to be caught:
    \do\{% <- give opening curly brace the verbatim-catcode again.
    \UD@collectverbarg{#1}{#2}{#3}%
  }%
}%
\newcommand\UD@collectverbarg[4]{%
  \do\ %             % <- Now that \UD@collectverbarg has the delimiter or
  \catcode`\^^I=12   %    emptiness in its 4th arg, give space and horizontal tab 
                     %    verbatim-catcode (again).
  \catcode`\^^M=12   % <- Give the carriage-return-character the verbatim-catcode.
  \long\def\@tempb##1#4{%
    \edef\@tempb{##1}%
    \@onelevel@sanitize\@tempb % <- Turn characters into their "12/other"-pendants.
                               %    This may be important with things like the 
                               %    inputenc-package which may make characters 
                               %    active/which give them catcode 13(active).
    \expandafter\UDEndlreplace\expandafter{\@tempb}{#1}{\def\@tempb}% <- this starts 
                               %    the loop for replacing endline-characters.
    \expandafter\UD@@collectverbarg\expandafter{\@tempb}{#2}{#3}% <- this "spits 
                               %    out the result.
  }%
  \@tempb
}%
\newcommand\UD@@collectverbarg[3]{%
  \endgroup
  \@esphack
  #2{#3{#1}}%
}%
\makeatother
%%<---------------- end of code for \UDcollectverbarg ----------------->

\documentclass{article}
\usepackage{minted}

% As a usage-example let's now define a macro \mintedcode which
% collects a verbatim-argument and wraps it into a listing- 
% and a minted-environment before passing it to \scantokens

\makeatletter
\newcommand\mintedcode[2][]{%
  \begingroup
  \def\@tempa{{#1}{#2}}%
  \@mintedcode
}%
\newcommand\@mintedcode[1][]{%
  \UDcollectverbarg{^^J}{\@firstofone}{%
    \expandafter\endgroup\expandafter\@@mintedcode\@tempa{#1}%
  }%
}%
\newcommand\@@mintedcode[1]{%
  \renewcommand\@@mintedcode[4]{%
    \scantokens{%
      \begin{listing}[htp]^^J
      \csname UD@CheckWhetherNull\endcsname{##1}{}{\caption{##1}}^^J
      \begin{minted}[framerule=0mm,
                     framesep=2mm,
                     frame=single,
                     rulecolor=\color{blue!30!white},
                     baselinestretch=1.2,
                     bgcolor=blue!30!white,
                     fontsize=\footnotesize,
                     ##3%
                    ]^^J%
      {##2}^^J##4^^J%
      #1^^J%
    }%
  }%
}%
\UDcollectverbarg{^^J}{\@firstofone}{\@@mintedcode}|\end{minted}
\end{listing}|

\makeatother

\begin{document}

\mintedcode[A caption]{bash}[breaklines=true]{
sudo apt-get install golang python3-dev python-dev libcupti-dev libjpeg-turbo8-dev \
make tmux htop chromium-browser git cmake zlib1g-dev libjpeg-dev  \
xvfb xorg-dev python-opengl libboost-all-dean v libsdl2-dev swig
}%

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보