如何在 lstlisting 上接受三种不同的语言

如何在 lstlisting 上接受三种不同的语言

如何接受三种语言的不同?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{xcolor,tikz}
\usepackage{showexpl} % exibe codigos de figuras

% Definindo novas cores
\definecolor{amarelo}{rgb}{1,1,0.8}
\definecolor{verde}{rgb}{0,0.5,0}
\definecolor{cinza}{rgb}{.9,.9,.9}

% Configurando layout para mostrar codigos LaTeX
\usepackage{listings}
% explpreset eh usado no showexpl
% configurando para codigos das figuras
\lstset{explpreset={
     language=[LaTeX]TeX,
     basicstyle=\ttfamily\small, 
     identifierstyle=\color{black}, 
     keywordstyle=\color{blue}, 
     stringstyle=\color{verde}, 
     commentstyle=\color{red}, 
     extendedchars=true, 
     showspaces=false, 
     showstringspaces=false, 
     numbers=none,
     %numberstyle=\tiny,
     breaklines=true, 
     backgroundcolor=\color{amarelo}, 
     breakautoindent=true, 
     captionpos=b,
     xleftmargin=0pt,
     frame=none,   %%frame=single
     rframe={}
  }, % configurando para codigos LaTeX
     language=[LaTeX]TeX,
     basicstyle=\ttfamily\small, 
     identifierstyle=\color{black}, 
     keywordstyle=\color{blue}, 
     stringstyle=\color{verde}, 
     commentstyle=\color{red}, 
     extendedchars=true, 
     showspaces=false, 
     showstringspaces=false, 
     numbers=left,
     numberstyle=\tiny, 
     breaklines=true, 
     backgroundcolor=\color{cinza}, 
     breakautoindent=true, 
     captionpos=b,
     xleftmargin=0pt,
}

\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc

\lstset{language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{javapurple}\bfseries,
stringstyle=\color{javared},
commentstyle=\color{javagreen},
morecomment=[s][\color{javadocblue}]{/**}{*/},
numbers=left,
numberstyle=\tiny\color{black},
numbersep=10pt,
tabsize=4,
showspaces=false,
showstringspaces=false}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{lipsum}
% Definindo novas cores
\definecolor{amarelo}{rgb}{1,1,0.8}
\definecolor{verde}{rgb}{0,0.5,0}
\definecolor{cinza}{rgb}{.9,.9,.9}
\begin{document}
 \lipsum
\end{document}
\end{lstlisting}

\begin{lstlisting}
public class HelloWorldApp {
     public static void main (String argv[])
     {
          // Display string
          System.out.println("Hello World!");
     }
}
\end{lstlisting}

% requer o pacote showexpl
\begin{LTXexample}[width=4cm]
\begin{tikzpicture}
  \draw circle (1);
\end{tikzpicture}
\end{LTXexample}

\end{document}

我需要 LaTeX、Java 和 LTXexample。但是 LaTeX 是灰色的,Java 是蓝色的,LTXexample 是黄色的。

答案1

使用alsolanguageand/或\lstdefinestyleand/或\lstnewenvironment

在此处输入图片描述

\documentclass[]{article}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}

\usepackage{showexpl}


\lstdefinestyle{LaTeX}
{
    language={[LaTeX]TeX},
    alsolanguage={PSTricks},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    explpreset={},
    morekeywords={
        graphicspath,
        includegraphics,
        blinddocument,
    },
}

\lstdefinestyle{Java}
{
    language={Java},
    alsolanguage={[Sharp]C},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{cyan!10},
    tabsize=2,
    columns=flexible,
    morekeywords={
        Console,
        WriteLine,
        int,
    },
}


\lstnewenvironment{java}
{\lstset{style=Java}}
{}

\begin{document}


\begin{LTXexample}[style=LaTeX,pos=r]
    \LaTeXe\ is not for children!
\end{LTXexample}

\begin{lstlisting}[style=Java]
    public void Main()
    {
        Console.WriteLine("Java is not for children!");
    }
\end{lstlisting}

\begin{java}
    public void Main(string[] args)
    {
        Console.WriteLine("Java is not for children!");
    }
\end{java}


\end{document}

相关内容