문자열에 따옴표가 포함된 목록 문제

문자열에 따옴표가 포함된 목록 문제

문자열 내부에 작은따옴표가 나타나면 listings문자열을 끝내는 것으로 해석되는 것 같습니다. 이런 일이 발생하지 않도록 하는 방법이 있나요?

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}

\setlength{\parindent}{0cm}

\usepackage[english]{babel}

\usepackage{listings}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\lstset{language=Matlab,
        basicstyle=\ttfamily,
        keywordstyle=\color{Blue}\ttfamily,
        stringstyle=\color{Red}\ttfamily,
        commentstyle=\color{Emerald}\ttfamily,
        morecomment=[l][\color{Magenta}]{\#},
        breaklines=true,
        breakindent=0pt,
        breakatwhitespace,
        columns=fullflexible,
        showstringspaces=false
}


\begin{lstlisting}
a=1;
if a==1
    disp('Is 1')
elseif a==2
    disp('Is 2')
else
    disp('I don't know')
end
\end{lstlisting}

\end{document}

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

답변1

작은따옴표는 Matlab에서 문자열 구분 기호로 사용되므로 해당 문자를 문자열의 일부로 만들려면 해당 문자를 이스케이프해야 합니다. 두 개의 작은따옴표를 연속으로 입력하면 됩니다.

disp('I don''t know')

Matlab에 의해 정의된 언어는 올바른 listings것을 조판할 만큼 영리합니다. 이 경우:

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

참고로 다음 사항에 관심이 있으실 수도 있습니다.matlab-prettifier패키지; 보다이 답변.

답변2

가능한 솔루션목록.pdf매개변수 포함 mathescape:

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}

\setlength{\parindent}{0cm}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{listings}




\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\lstset{language=Matlab,
        basicstyle=\ttfamily,
        keywordstyle=\color{Blue}\ttfamily,
        stringstyle=\color{Red}\ttfamily,
        commentstyle=\color{Emerald}\ttfamily,
        morecomment=[l][\color{Magenta}]{\#},
        breaklines=true,
        breakindent=0pt,
        breakatwhitespace,
        columns=fullflexible,
        showstringspaces=false
}


\begin{lstlisting}[mathescape]
a=1;
if a==1
    disp('Is 1')
elseif a==2
    disp('Is 2')
else
    disp('I don$\texttt{\color{red}'}$t know')
end
\end{lstlisting}

\end{document}

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

관련 정보