자신의 환경에서 특수 문자를 자동으로 이스케이프 처리하는 방법은 무엇입니까?

자신의 환경에서 특수 문자를 자동으로 이스케이프 처리하는 방법은 무엇입니까?

소스 코드 예제를 위한 환경을 만들어야 합니다. 나는 그것이 다음과 같이 보이기를 원합니다 :

코드 예:

------------------------------------------------

# 코드 예시입니다

에코 '안녕하세요'

------------------------------------------------


문제는 특수 문자(특히 해시)를 자동으로 이스케이프 처리하여 사용자가 해시를 작성할 때마다 백슬래시를 입력할 필요가 없도록 하는 방법입니다.


내 코드

\def\terminalText#1\end{\hspace*{2em}\texttt{#1}\\\end}

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\terminalText}
{\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}

이 코드는 잘 작동합니다. 환경 내부에 특수 문자를 작성하려고 할 때까지 다음 오류가 발생합니다.

! Paragraph ended before \terminalText was complete.


verbatim질문에 따르면 나는 환경에 대한 일종의 포장지를 만들려고 노력했습니다.새로운 환경 정의에 그대로 사용할 수 있습니까?, 하지만 다음과 같은 오류가 발생합니다.

! LaTeX Error: \begin{codeExample} on input line 535 ended by \end{verbatim}.

내 두 번째 코드

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\verbatim
}
{%
\endverbatim
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}

답변1

패키지를 사용하면 verbatim두 번째 예제의 코드가 즉시 작동합니다( dashrule패키지도 포함된 경우).

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}


\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}




\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\end{document}

여기에 이미지 설명을 입력하세요

listings코드 예제를 조판하는 '더 좋고' 깔끔한 방법으로 패키지를 고려하십시오 .

편집하다tcolorbox다음은 목록 출력 의 예입니다 .

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}

\usepackage[most]{tcolorbox}


\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}


\newtcblisting[auto counter]{codeex}[1][]{%
  arc=0pt,
  auto outer arc,
  colbacktitle=yellow,
  coltitle=black,
  title={Code Example \thetcbcounter},
  listing options={language=bash},
  listing only,
  lowerbox=ignored,
  before upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm},
  after upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm}
  #1
}

\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\begin{codeex}
echo 'Hello' 

\end{codeex}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보