最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pdflatex - How to define block custom command in latex - Stack Overflow

programmeradmin0浏览0评论

I'm using overleaf, I want to create a \codewithcustom command that will be used like

\codewithcustom{cpp}{some caption}{
 int a[10];
}

but I always get fancy verb error. Can you please help me convert the code-block syntax into easier custom command? Also, here's a minimal reproducible example.

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{xparse}
\definecolor{LightGray}{gray}{0.98}

% Global minted settings
\setminted{frame=none, 
breaklines=true,
framesep=5mm,
fontsize=\large,
baselinestretch=1.2,
bgcolor=LightGray,
tabsize=2,
linenos}


\title{C++ \& Python Personal Notes}
\author{Rakesh Shrestha}
\date{January 2025}
\begin{document}
\maketitle

\section{Vectors \& raw Arrays}
\subsection{Initialization}

% I want to create this to be used as a custom command
\begin{listing}[!ht]
\begin{minted}{cpp}
vector<vector<int>> bucket(maxFreq + 1);
for(const auto& a: hmap){
    bucket[a.second].push_back(a.first);
}
\end{minted}
\caption{Initialize empty elements with specified size in vectors}
\label{listing: 1}
\end{listing}

\begin{listing}[!ht]
\begin{minted}{cpp}
vector<vector<int>> bucket(10, 2);
\end{minted}
\caption{Initialize specified vectors with data}
\end{listing}


\end{document}

What I tried,

\newcommand{\codewithcaption}[3]{
  \begin{listing}
    \begin{minted}{#1}
     #3 
    \end{minted}}
    \caption{#2}
   \end{listing

I'm using overleaf, I want to create a \codewithcustom command that will be used like

\codewithcustom{cpp}{some caption}{
 int a[10];
}

but I always get fancy verb error. Can you please help me convert the code-block syntax into easier custom command? Also, here's a minimal reproducible example.

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{xparse}
\definecolor{LightGray}{gray}{0.98}

% Global minted settings
\setminted{frame=none, 
breaklines=true,
framesep=5mm,
fontsize=\large,
baselinestretch=1.2,
bgcolor=LightGray,
tabsize=2,
linenos}


\title{C++ \& Python Personal Notes}
\author{Rakesh Shrestha}
\date{January 2025}
\begin{document}
\maketitle

\section{Vectors \& raw Arrays}
\subsection{Initialization}

% I want to create this to be used as a custom command
\begin{listing}[!ht]
\begin{minted}{cpp}
vector<vector<int>> bucket(maxFreq + 1);
for(const auto& a: hmap){
    bucket[a.second].push_back(a.first);
}
\end{minted}
\caption{Initialize empty elements with specified size in vectors}
\label{listing: 1}
\end{listing}

\begin{listing}[!ht]
\begin{minted}{cpp}
vector<vector<int>> bucket(10, 2);
\end{minted}
\caption{Initialize specified vectors with data}
\end{listing}


\end{document}

What I tried,

\newcommand{\codewithcaption}[3]{
  \begin{listing}
    \begin{minted}{#1}
     #3 
    \end{minted}}
    \caption{#2}
   \end{listing
Share Improve this question edited Feb 3 at 10:30 rakesh shrestha asked Feb 3 at 9:46 rakesh shrestharakesh shrestha 1,4553 gold badges23 silver badges39 bronze badges 4
  • Please add a compilable minimal reproducible example – samcarter_is_at_topanswers.xyz Commented Feb 3 at 9:58
  • @samcarter_is_at_topanswers.xyz I added a command that I tried and updated the latex command to be much more simpler – rakesh shrestha Commented Feb 3 at 10:24
  • Please add a minimal reproducible example which includes a class, the necessary packages etc. We can't compile your code fragments. – samcarter_is_at_topanswers.xyz Commented Feb 3 at 10:25
  • @samcarter_is_at_topanswers.xyz Thanks for the suggestions. I've updated the description. – rakesh shrestha Commented Feb 3 at 10:33
Add a comment  | 

1 Answer 1

Reset to default 0

Using fragile material, like verbatim code, in the argument of a macro isn't the best of ideas. Better use an environment:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{LightGray}{gray}{0.98}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}

\AtBeginDocument{
  \newtcblisting[blend into=listings]{codewithcustom}[2]{
    listing engine=minted,
    minted language=#1,
    title=#2,
    detach title,
    coltitle=black,
    listing and comment,
    comment=\centering\tcbtitle,
    oversize,
    enhanced,
    interior hidden,
    frame hidden,
    segmentation hidden,
    minted options={
        breaklines=true,
        framesep=5mm,
        fontsize=\large,
        baselinestretch=1.2,
        bgcolor=LightGray,
        tabsize=2,
        linenos
    }
  }
}

\begin{document}

\begin{codewithcustom}{cpp}{Initialize empty elements with specified size in vectors}
vector<vector<int>> bucket(maxFreq + 1);
for(const auto& a: hmap){
    bucket[a.second].push_back(a.first);
}
\end{codewithcustom}


\begin{codewithcustom}{cpp}{Initialize empty elements with specified size in vectors}
vector<vector<int>> bucket(maxFreq + 1);
for(const auto& a: hmap){
    bucket[a.second].push_back(a.first);
}
\end{codewithcustom}


\end{document}

发布评论

评论列表(0)

  1. 暂无评论