
패키지 에 대해 알고 있지만 listings
원하는 위치( h
in float)에 놓을 수 없는 것 같습니다.
그리고 정확히 한 줄의 코드를 삽입하고 싶을 수도 있으므로 코드를 넣은 위치에 꼭 삽입하고 싶은데 좋은 방법이 없을까요?
답변1
옵션 1: 패키지 listings
사용
LaTeX 헤더에 대한 간단한 구성(이전 \begin{document}
):
\usepackage{listings}
\usepackage{color}
\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={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true
tabsize=3
}
를 사용하면 문서 중간에 기본 언어를 변경할 수 있습니다 \lstset{language=Java}
.
문서에서의 사용 예:
\begin{lstlisting}
// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class Hello extends JApplet
{
public void paintComponent(Graphics g) {
g.drawString("Hello, world!", 65, 95);
}
}
\end{lstlisting}
결과는 다음과 같습니다.
옵션 2: verbatim
환경 사용
\begin{verbatim}
your
code
example
\end{verbatim}
답변2
이 numberedblock
패키지는 짧은 코드 블록을 표시하기 위한 것입니다. 패키지 사용 예에서:
\documentclass[10pt]{article}
\usepackage{numberedblock}
\begin{document}
\parindent 0.3in
%\setlength\maxblocklabelsize{-.4in}
\setlength\blockindent{0.0in}
This is a test of the \textsf{numberedblock} style packcage, which is
specially designed to produce sequentially numbered BLOCKS of code (note
the individual code lines are not numbered, but the whole block gets a
single number, for later reference (much in the same way that equations
can get numbered in a document). While specialized for numbering code
blocks, the commands can actually number other items, as well, in fact
anything that fits in a \LaTeX{} box.
If the code block contains no special characters, one can simply use the
command form, called \verb,\numblock,. It cannot handle verbatim text,
but must use standard \LaTeX{} escape sequences (for line breaks,
contiguous spaces, special characters, etc.). It puts the output in a
tt font , which is the same used in the verbatim environment:
\numblock{This text is the\\argument to the command\\where double
slashes have been\\used for line breaks}
Most useful, however, there is also the \verb,numVblock, environment,
which handles verbatim text, as seen in the next example:
\begin{numVblock}
This is the numVblock
environment, which (<--see contiguous spaces here)
succeeds in
incorporating verbatim text like
@##$%*$%$()||}{?><\\ \end{numVblock}
As envisioned the \verb,numVblock, environment would be ideally suited
for displaying small code blocks as part of documentation. The code can
contain contiguous spaces and special characters:
\begin{numVblock}
program test
implicit none
integer a, x
c$$$$$$$$$$$$$$$$$$$$$$$$$
a = 0
x = 1
10 a = a + x
if (a .eq. 100) stop
goto 10
end
\end{numVblock}
Below, I test the \verb,\numblock, command with the argument as a
box, rather than as formatted text.
\numblock{\fbox{Testing, 1,2,3 testing a box}}
Don't forget, there are settable parameters to define the block
left-indent, the format of the label, and (if needed) the labels' max
width/placement.
\end{document}