使用列表和解析对字符串和变量之外的数字进行着色无法按预期工作。需要帮助修复 PDFLaTeX

使用列表和解析对字符串和变量之外的数字进行着色无法按预期工作。需要帮助修复 PDFLaTeX

我正在写论文,需要将代码摘录放入其中。我在 Visual Studio 中开发了所有 C# 代码,希望颜色大致相同,因此我采用了本网站上所有问题中发现的不同方法。

这是我得到的:

代码示例 一些着色数字的例子,0,2 着色使用转义字符

这是我所期望的:

在此处输入图片描述 在此处输入图片描述

问题

与描述的一样这里但是 PDFLaTeX 并没有解决该问题,因此我无法在我的项目中使用其中一种解决方案。

编辑:简而言之,如果出现以下情况,我希望突出显示数字:

它们位于下列任意字符之后:,.,(,{,[,:它们位于下列任意字符之前:,.,),},],:,;但绝对不包括在任一组中的 _,因为这可能会破坏许多代码片段中的变量命名约定(即使 Python 确实接受数字中的 _ 以帮助直观识别 10^3 的组,如 x = 1_000_000。

具体来说,我希望能够将浮点数“f”着色为类似数字的颜色,并能够自动为后跟逗号的数字着色。

这是我的工作代码:

关于上面提到的问题的代码,我的代码在解析部分进行了编辑以实现更具体的功能,并且有一个 patchcmd 将 ) 包含到解析中,也使用 breaklines=true。

[请阅读样式定义中的注释,以了解我的一些问题和选择。可能正确,也可能错误。

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = pdflatex
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{etoolbox}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter

%%% Copied from https://tex.stackexchange.com/a/500690/23765
% Some conditional tests
\def\@genericif#1{#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\def\@ifdigit#1{\@genericif{\ifnum1<1\noexpand#1\relax}}
\def\@ifempty#1{\@genericif{\if\relax\detokenize{#1}\relax}}

% The main parsing macros
\def\parse@num#1{%
    \@ifempty{#1}%
    {\parse@num@false}%
    {\@genericif{\parsesign}%
        {\parse@num@sign#1{}\@end}%
        {\parse@num@dig#1{}\@end}%
    }%
}
% Parse sign
\def\parse@num@sign#1#2\@end{%
    \@genericif{\ifx\parse@num@minus#1}%
    {\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
    {\@genericif{\ifx\parse@num@plus#1}%
        {\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
        {\@genericif{\ifx\parse@num@div#1}%
            {\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
            {\@genericif{\ifx\parse@num@mul#1}%
                {\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
                {\parse@num@dig#1#2\@end}%
            }%
        }%
    }%
}
% Parse first digit
\def\parse@num@dig#1#2\@end{%
    \@ifdigit{#1}%
    {\@ifempty{#2}{\parse@num@true}{\parse@num@digs#2\@end}}%
    {\parse@num@false}%
}
% Parse optional following digits
\def\parse@num@digs#1#2\@end{%
    \@ifdigit{#1}{%
        \@ifempty{#2}%
        {\parse@num@true}%
        {\parse@num@digs#2\@end}%
    }{%
        \@genericif{\parsefloat}{%
            \@genericif{\ifx\parse@num@point#1}%
            {\@ifempty{#2}{\parse@num@false}{\parse@num@decs#2\@end}}%
            {\@genericif{\ifx\parse@num@comma#1}%
                {\@ifempty{#2}{\parse@num@true}{\parse@num@decs#2\@end}}%
                {\parse@num@fleffe#1\@end}%
            }%
        }%
        {\parse@num@false}%
    }%
}

% Parse decimal places
\def\parse@num@decs#1#2\@end{%
    \@ifdigit{#1}{%
        \@ifempty{#2}%
        {\parse@num@true}%
        {\@genericif{\ifx\parse@num@comma#2}%
            {\parse@num@false}% 
            {\parse@num@endings#2\@end}%
        }%
    }%
    {\@genericif{\ifx\parse@num@comma#1}%
        {\@ifempty{#2}{\parse@num@true}{\parse@num@false}}%
        {\parse@num@fleffe#1\@end}%
    }%
}


% parse f
\def\parse@num@fleffe#1#2\@end{%
    \@genericif{\ifx\parse@num@effe#1}{%
        \@ifempty{#2}%
        {\parse@num@true}%
        {\parse@num@digs#2\@end}%
    }{\parse@num@false}%
}

%parse after first digit of decs
\def\parse@num@endings#1#2\@end{%
    \@ifdigit{#1}{%
        \@ifempty{#2}%
        {\parse@num@true}%
        {\parse@num@fleffe#2\@end}%
    }%
    {\parse@num@fleffe#1\@end}%
}

% User interface
\newcommand\ifnumber[4][]{%
    \begingroup
    \let\parsesign=\iftrue
    \let\parsefloat=\iftrue
    \let\parse@num@minus=-%
    \let\parse@num@plus=+%
    \let\parse@num@point=.%
    \let\parse@num@effe=f%
    \let\parse@num@div=/%
    \let\parse@num@mul=*%
    \let\parse@num@comma=,%
    #1%
    \def\parse@num@true{\endgroup#3}%
    \def\parse@num@false{\endgroup#4}%
    \parse@num{#2}%
}   


%%% Additions to the listings package
\lst@Key{numbersstyle}{}{\def\lst@numbersstyle{#1}}
\lst@Key{parsenumbers}{false}[t]{\lstKV@SetIf{#1}\lst@ifparsenumbers}

\lst@AddToHook{OutputOther}{%
    \lst@ifparsenumbers
    % Only if mode changes are not prohibited
    \lst@ifmode\else
    \expandafter\@hook@ifnumber\the\lst@token\@end
    {\let\lst@thestyle=\lst@numbersstyle}%
    {}%
    \fi
    \fi
}
\def\@hook@ifnumber#1#2\@end{%
    \@genericif{\ifx\lst@nolig#1}%
    {\@hook@ifnumber@{#2}}%
    {\@hook@ifnumber@{#1#2}}%
}

\def\@hook@ifnumber@{%
    \ifnumber[\expandafter\let\expandafter\parse@num@minus\csname lst@um-\endcsname]%
}

\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{lillagithub}{RGB}{177,104,222}
\definecolor{typeColor}{RGB}{134, 198, 145}%{134, 193, 120}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%colori VS mio
%white -> default white for black backgorunds %normal text, operators, expression, parenthesis
\definecolor{blueVS}{RGB}{53, 131, 213} %access modifier, method parameters, acess, literal, contextual, query
\definecolor{violetVS}{RGB}{200,160,219}%{176,156,223} %statement, iterator
\definecolor{tealVS}{RGB}{58, 176, 166} %namespace, class name
\definecolor{cyanVS}{RGB}{104,198,252} %variables
\definecolor{paleyellowVS}{RGB}{220,220,170} %methods
\definecolor{orangeVS}{RGB}{214,157,133} %strings
\definecolor{blackBackgroundVS}{RGB}{30,30,30} %background dark theme
\definecolor{greenfinchVS}{RGB}{77,166,66} %comments
\definecolor{lightGreenVS}{RGB}{179, 206, 153} %numbers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\patchcmd{\lsthk@SelectCharTable}{)}{`}{}{} % to color number with closed roun parenthesis 
\makeatother 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lstdefinestyle{CSstyle} {
    language=[Sharp]C,
    columns=flexible,
    basicstyle=\footnotesize\ttfamily\color{white}, 
    numbers=left,
    numberstyle=\tiny\color{blueVS}, 
    numbersep=5pt,
    tabsize=2,
    extendedchars=true,
    breaklines=true, %%%%%%%%%%%
    frame=b, 
    linewidth=150mm,
    stringstyle=\color{orangeVS}\ttfamily,
    showspaces=false,
    showtabs=false,
    xleftmargin=17pt,
    framexleftmargin=17pt,
    framexrightmargin=5pt,
    framexbottommargin=0pt,
    commentstyle=\color{greenfinchVS},
    morecomment=[l]{//}, %use comment-line-style!
    morecomment=[s]{/*}{*/}, %for multiline comments
    showstringspaces=false,
    backgroundcolor=\color{blackBackgroundVS},
    identifierstyle=\color{cyanVS}, 
    keywordstyle=\color{blueVS},
    escapeinside={@@},
    parsenumbers = true,
    numbersstyle=\color{lightGreenVS}, 
    classoffset=0,
    morekeywords={abstract, async, const, event, extern, in, new, out, override, readonly, sealed, static, unsafe, virtual, volatile, public, protected, internal, private, params, in, ref, out, class, interface, struct, enum, delegate, namespace, extern, alias, null, true, false, default, add, and, async, await, dynamic, get, global, init, nint, not, nuint, or, partial, record, remove, set, value, var, when, where, checked, unchecked, fixed, lock, continue, bool, byte, sbyte, char, decimal, double, float, int, uint, nint, nuint, long, ulong, short, ushort, bool, byte, sbyte, char, decimal, double, float, int, uint, nint, nuint, long, ulong, short, ushort},
    classoffset=1,
    morekeywords={Vector2,Vector3,Bounds},
    keywordstyle=\color{typeColor},
    classoffset=2,
    emph={using, while, yield, return, break, throw, try, catch, finally, if, switch, do, for, foreach, while, goto},
    emphstyle=\color{violetVS},
    classoffset = 3,
    otherkeywords={;,=,!,!=,~,\&,+,-,\^,\%,<-,<<-,>,>=,<,<=,(,),[,],\{,\},\\,|}, %cannot add / * because are used for comment
    morekeywords={;,=,!,!=,~,\&,+,-,\^,\%,<-,<<-,\_,/,>,>=,<,<=,(,),[,],\{,\},\\,|}, %if i add comma to other and more keywords, it will always be of the same color of keyword[0] (blueVS), but if i add it here, the number is recognized from the parsing algorithm
    keywordstyle=\color{white},
    classoffset=4,
    morekeywords={CalculateBoundWorlds,TransformPoint, GetComponent,OnMouseDrag,OnMouseDown,OnMouseUp,DragCamera,setLastPosView,ScreenToViewportPoint,Limit2Bound,Translate},
    keywordstyle=\color{paleyellowVS},
    classoffset=6,
    morekeywords={Camera,BackgroundBoundsCalculator, MonoBehaviour, Tooltip, HideInInspector, SerializeField, SpriteRenderer,Sprite},
    keywordstyle=\color{tealVS}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{blue}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

    \begin{lstlisting}[language={[Sharp]C}, caption={C\# exaple}, label={Script}, style=CSstyle]
        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        public class BackgroundBoundsCalculator : MonoBehavior
        {
            /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/
            public Vector2[] CalculateBoundWorlds()
            {
                string s = "stringa"
                SpriteRenderer m_SpriteRenderer = @[email protected]<SpriteRenderer>();
                Sprite sprite = m_SpriteRenderer.@sprite@;
                
                Bounds bounds = sprite.@bounds@;
                Vector3 max, min;
                max = bounds.@max@;
                min = bounds.@min@;
                
                Vector3 xBound = @[email protected](min.@x@, max.@x@, 0);
                Vector3 yBound = @[email protected](min.@y@, max.@y@, 0);
                
                //v1
                Vector2[] boundWorlds = new Vector2[2];
                boundWorlds[0] = new Vector2(xBound.@x@, xBound.@y@);
                boundWorlds[1] = new Vector2(yBound.@x@, yBound.@y@);
                if(){}
                return boundWorlds;
                
            }
        }
    
        
        var d = 5f;
        Vector3 v3 = new Vector3(1, 2f, 3);     
        (1) [2] {3} <4> |5| \6\ /7/ !8! (9} {10]    
        
        0.1 0,2 0, 2, 2. .2 ,7 *3*
        0@,@2
        2@,@
        +1 -2 *3 /4
        float prova = 4.5;
        3.4f 5f
        .3. 6*
        0@f@
        //5
        ///
        4.20f,
        27,
        27f.
    \end{lstlisting}
    
    \pagebreak
    \newcommand\testnum[2][]{%
        \makebox[5em][l]{#2}%
        \makebox[5em][l]{\ifnumber[#1]{#2}{yes}{no}}%
    }
    \ttfamily
    default setting\par
    \testnum{}
    \testnum{0}\par
    \testnum{1}
    \testnum{12345}\par
    \testnum{+}
    \testnum{+123}\par
    \testnum{-}
    \testnum{-123}\par
    \testnum{-0}
    \testnum{+0}\par
    \testnum{0.}
    \testnum{0.123}\par
    \testnum{-.1}
    \testnum{-+123}\par
    \testnum{-0.123}
    \testnum{+0.123}\par
    \testnum{abc}
    \testnum{123abc}\par
    
    
    \medskip
    \makeatletter
    \string\modificaMia\par
    \testnum[]{-123,45}
    \testnum[]{0,}\par
    \testnum[]{,2}
    \testnum[]{4.20f,}\par
    \testnum[]{3.}
    \testnum[]{7.2f}\par
    \testnum[]{/6}
    \testnum[]{*3}\par
    \testnum[]{+1}
    \testnum[]{7f}\par
    \testnum[]{6,3}
    \testnum[]{7,3,}\par
    \testnum[]{+1.8f}
    \testnum[]{27,5f}\par
    \testnum[]{123,4f}
    \testnum[]{?}\par
    \makeatother
\end{document}

输出:

  • 以上两张预期截图
  • 测试部分,我保证解析工作正常,如下所示

在此处输入图片描述

正如您在最后一张截图中看到的,有多种解析数字的方法。特别是,我希望看到所有“表示是”的数字都被标记为“f”(包括)。当最后一个字符是逗号时,不是逗号(意思是像 new Vector(1, 2, 3) 中的参数一样的普通逗号);

简而言之,我希望获得与第二张预期屏幕截图相同的结果。

请帮助我,我认为一旦完成,此代码对于希望在其项目中使用与 VisualStudio 相同的调色板的每个人来说也将很有用,事实上,我原本打算在完成后将其发布在我的 githbub 上,但我还没能完成它...

相关内容