使用 tcolorbox 列出 CodeBlocks

使用 tcolorbox 列出 CodeBlocks

有没有办法列出此代码

     #include<stdio.h>
     int main (void)
     {
        int l=0; //intero che usiamo per calcolare la lunghezza del messaggio
        //adesso faccio un while in cui chiedo continuamente caratteri
        while(getchar()!='\n')  l++;

        //STAMPA DELL'OUTPUT
         printf("\n il tuo messaggio e' lungo %d caratteri",l);

        return 0;
        }

为了拥有这种 CodeBlocks 风格?

在此处输入图片描述

现在,我有这个设置

    \lstset{
    %vertical space
    aboveskip=0cm,
    belowskip=0cm,
    language=C, 
    %keywords-----------------------------------------------
    keywordstyle=\bfseries\color{blue}, 
    %otherkeywords={}                  % if you want to add more keywords to   the set
    %numbers------------------------------------------------
     numbers=left,                      % where to put the line-numbers;      possible values are (none, left, right)
     numbersep=0.1cm,                   % how far the line-numbers are from the code
     numberstyle=\tiny\color{black},    % the style that is used for the line-numbers  
     %comments-----------------------------------------------
      commentstyle=\color{gray},
      stringstyle=\color{blue!70},
      escapeinside={(*}{*)},          % if you want to add LaTeX within your code
      morecomment=[l][\color{green!60!black}]{\#},  
      literate=*{0}{{\textcolor{magenta}{0}}}{1}%
         {1}{{\textcolor{magenta}{1}}}{1}%
         {2}{{\textcolor{magenta}{2}}}{1}%
         {3}{{\textcolor{magenta}{3}}}{1}%
         {4}{{\textcolor{magenta}{4}}}{1}%
         {5}{{\textcolor{magenta}{5}}}{1}%
         {6}{{\textcolor{magenta}{6}}}{1}%
         {7}{{\textcolor{magenta}{7}}}{1}%
         {8}{{\textcolor{magenta}{8}}}{1}%
         {9}{{\textcolor{magenta}{9}}}{1}%
         {.0}{{\textcolor{magenta}{.0}}}{2}%
         {.1}{{\textcolor{magenta}{.1}}}{2}%
         {.2}{{\textcolor{magenta}{.2}}}{2}%
         {.3}{{\textcolor{magenta}{.3}}}{2}%
         {.4}{{\textcolor{magenta}{.4}}}{2}%
         {.5}{{\textcolor{magenta}{.5}}}{2}%
         {.6}{{\textcolor{magenta}{.6}}}{2}%
         {.7}{{\textcolor{magenta}{.7}}}{2}%
         {.8}{{\textcolor{magenta}{.8}}}{2}%
         {.9}{{\textcolor{magenta}{.9}}}{2}%
         {'.'}{{\textcolor{orange}{'.'}}}{3}
         {=}{{\textcolor{red}{=}}}{1}%
         {>}{{\textcolor{red}{>}}}{1}%
         {<}{{\textcolor{red}{<}}}{1}%
         {*}{{\textcolor{red}{*}}}{1}%
         %{/}{{\textcolor{red}{/}}}{1}% stuffs up comments
         {!}{{\textcolor{red}{!}}}{1}
         {|}{{\textcolor{red}{|}}}{1}
         {+}{{\textcolor{red}{+}}}{1}%
         {-}{{\textcolor{red}{-}}}{1}%
         {\%}{{\textcolor{red}{\%}}}{1}%
         {:}{{\textcolor{red}{:}}}{1}%
         {;}{{\textcolor{red}{;}}}{1}%
         {,}{{\textcolor{red}{,}}}{1}%
         {\&}{{\textcolor{red}{\&}}}{1}%
         {(}{{\textcolor{red}{(}}}{1}%
         {)}{{\textcolor{red}{)}}}{1}%
         {\{}{{\textcolor{red}{\{}}}{1}%
         {\}}{{\textcolor{red}{\}}}}{1},
        title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title             
         }

     \newtcbinputlisting\codep[1]
     {
      listing file={#1},
      title={#1},
      %stile del paragrafo
      colback=black!5!white,  %sfondo
      colframe=black!25,      %bordi
      %opzioni per il codice
      listing only,
      listing options={language=C} 
      }

这是我的输出:

在此处输入图片描述

这是我的意见:

    \codep{LunghezzaDiUnMessaggio.c}

将 LunghezzaDiUnMessaggio.c 保存在.c 文件中。

答案1

在您的代码中,您language=C第二次使用了\newtcbinputlisting\codep。这会将一些颜色设置重置为正常,尤其是绿色注释。

省略language=C可得到预期结果。

在此处输入图片描述

由于您没有提供 MWE,我花了一些时间创建自己的 MWE 来模拟这种情况。我在代码中标记了关键更改:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[margin=1in]{geometry}
\usepackage[most]{tcolorbox}


\begin{filecontents*}{LunghezzaDiUnMessaggio.c}
#include<stdio.h>
int main (void)
{
  int l=0; //intero che usiamo per calcolare la lunghezza del messaggio
  //adesso faccio un while in cui chiedo continuamente caratteri
  while(getchar()!='\n')  l++;

  //STAMPA DELL'OUTPUT
   printf("\n il tuo messaggio e' lungo %d caratteri",l);

  return 0;
}
\end{filecontents*}


\lstset{
  %vertical space
  aboveskip=0cm,
  belowskip=0cm,
  language=C,
  %keywords-----------------------------------------------
  keywordstyle=\bfseries\color{blue},
  %otherkeywords={}                  % if you want to add more keywords to   the set
  %numbers------------------------------------------------
  numbers=left,                      % where to put the line-numbers;      possible values are (none, left, right)
  numbersep=0.1cm,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{black},    % the style that is used for the line-numbers
  %comments-----------------------------------------------
  commentstyle=\color{gray},
  stringstyle=\color{blue!70},
  escapeinside={(*}{*)},          % if you want to add LaTeX within your code
  morecomment=[l][\color{green!60!black}]{\#},
  literate=*{0}{{\textcolor{magenta}{0}}}{1}%
     {1}{{\textcolor{magenta}{1}}}{1}%
     {2}{{\textcolor{magenta}{2}}}{1}%
     {3}{{\textcolor{magenta}{3}}}{1}%
     {4}{{\textcolor{magenta}{4}}}{1}%
     {5}{{\textcolor{magenta}{5}}}{1}%
     {6}{{\textcolor{magenta}{6}}}{1}%
     {7}{{\textcolor{magenta}{7}}}{1}%
     {8}{{\textcolor{magenta}{8}}}{1}%
     {9}{{\textcolor{magenta}{9}}}{1}%
     {.0}{{\textcolor{magenta}{.0}}}{2}%
     {.1}{{\textcolor{magenta}{.1}}}{2}%
     {.2}{{\textcolor{magenta}{.2}}}{2}%
     {.3}{{\textcolor{magenta}{.3}}}{2}%
     {.4}{{\textcolor{magenta}{.4}}}{2}%
     {.5}{{\textcolor{magenta}{.5}}}{2}%
     {.6}{{\textcolor{magenta}{.6}}}{2}%
     {.7}{{\textcolor{magenta}{.7}}}{2}%
     {.8}{{\textcolor{magenta}{.8}}}{2}%
     {.9}{{\textcolor{magenta}{.9}}}{2}%
     {'.'}{{\textcolor{orange}{'.'}}}{3}
     {=}{{\textcolor{red}{=}}}{1}%
     {>}{{\textcolor{red}{>}}}{1}%
     {<}{{\textcolor{red}{<}}}{1}%
     {*}{{\textcolor{red}{*}}}{1}%
     %{/}{{\textcolor{red}{/}}}{1}% stuffs up comments
     {!}{{\textcolor{red}{!}}}{1}
     {|}{{\textcolor{red}{|}}}{1}
     {+}{{\textcolor{red}{+}}}{1}%
     {-}{{\textcolor{red}{-}}}{1}%
     {\%}{{\textcolor{red}{\%}}}{1}%
     {:}{{\textcolor{red}{:}}}{1}%
     {;}{{\textcolor{red}{;}}}{1}%
     {,}{{\textcolor{red}{,}}}{1}%
     {\&}{{\textcolor{red}{\&}}}{1}%
     {(}{{\textcolor{red}{(}}}{1}%
     {)}{{\textcolor{red}{)}}}{1}%
     {\{}{{\textcolor{red}{\{}}}{1}%
     {\}}{{\textcolor{red}{\}}}}{1},
    title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

\newtcbinputlisting\codep[1]
{
  listing file={#1},
  title={#1},
  %stile del paragrafo
  colback=black!5!white,  %sfondo
  colframe=black!25,      %bordi
  %opzioni per il codice
  listing only,
  listing options={}% <---------------- HERE -----------------
}


\begin{document}

\codep{LunghezzaDiUnMessaggio.c}

\end{document}

相关内容