latex 插入java 代码模版

废了好大劲
把下面那堆东西添加到头文件就好了,(PS :( UI真难做)

模版

 \usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
     language=Java,
     aboveskip=3mm,
     belowskip=3mm,
     showstringspaces=false,
     columns=flexible,
     basicstyle = \ttfamily\small,
     numbers=none,
     numberstyle=\tiny\color{gray},
     keywordstyle=\color{blue},
     commentstyle=\color{dkgreen},
     stringstyle=\color{mauve},
     breaklines=true,
     breakatwhitespace=true,
     tabsize=3
}

样例


\begin{lstlisting}[ language=Java]
package information;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public final class Graph {
    private final static String curDir = System.getProperty("user.dir")+System.getProperty("file.separator")+"output";
    private final static String sysLineSep = System.getProperty("line.separator");
    private final static byte[] lsByteArr = sysLineSep.getBytes();
    private ArrayList<String> edges = new ArrayList<>();
    private ArrayList<String> nodes = new ArrayList<>();

    public Graph() {
        super();
    }

    public void addEdge(int from,int to,Character c) {
        this.addEdge(String.valueOf(from),String.valueOf(to), String.valueOf(c));
    }
    public void addEdge(String from,String to, String c) {
        edges.add(from+" -> "+to+String.format("[label=\"%s\"]", c));
    }
    private File writeDotFile(String name) {
        File dotFile = new File(curDir, name+".dot");

        // write dot file
        try(FileOutputStream fos = new FileOutputStream(dotFile)){
            fos.write(String.valueOf("digraph tmp{").getBytes());
            fos.write(lsByteArr);
            writeNodes(fos);
            writeEdges(fos);
            fos.write(String.valueOf("}").getBytes());
            fos.write(lsByteArr);
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return dotFile;
    }
    private void writeEdges(OutputStream os) throws IOException {
        for(String e: edges){
            os.write(e.getBytes());
            os.write(lsByteArr);
        }
    }
    private void writeNodes(OutputStream os) throws IOException {
        for(String e: nodes){
            os.write(e.getBytes());
            os.write(lsByteArr);
        }
    }
    public void draw(String name) {
        File dotFile = writeDotFile(name);
        File png = new File(curDir,name+".png");
        String cmd = String.format("dot %s -T png -o %s", dotFile.getAbsolutePath(),png.getAbsolutePath());
        try {
            Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void draw() {
        String name = "graph";
        draw(name);
    }
    public void setStart(int node) {
        nodes.add(String.format("%d [color=blue]", node));
    }
    public void setEnd(int node) {
        nodes.add(String.format("%d [color=red]", node));
    }

}

\end{lstlisting}

输出效果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/dylan_frank/article/details/80385349