リスト内の `std::cout` の前後にスペースを入れないでください

リスト内の `std::cout` の前後にスペースを入れないでください

この例では、

\documentclass{article}
\usepackage{listings}

\lstset{
    language=c++,
    extendedchars=true,
    inputencoding=utf8,
    literate={::}{{::}}2
}

\begin{document}

\begin{lstlisting}
std::mutex mtx;
\end{lstlisting}

\end{document}

私が得たのは ですstd :: mutex mtx;。リスト モードではこれらの自動スペースは必要ありません。

無効にする方法はありますか?

答え1

これは固定列のデフォルト設定、つまりすべての文字が同じ幅になっていることによるものです。:文字の幅があまり広くないため、スペースが挿入されているように見えます。

回避策としては、 の文字幅を::1 文字、つまり に設定することですliterate={::}{{::}}1。それ以外の場合は、フレキシブル列またはフルフレキシブル列を使用できます。別のオプションとしては、テレタイプ フォントを使用する方法があります。この場合は、::文字の幅が広く、均等に間隔が空くため、余分なスペースが挿入されているようには見えません。

MWE:

\documentclass{article}
\usepackage{listings}

\lstset{frame=single}
\begin{document}
\noindent \textit{Flexible columns:}
\begin{lstlisting}[columns=flexible]
std;;mutex mtx;
wwwwwwwwwwwwww;

std::mutex mtx;
\end{lstlisting}
\noindent \textit{Full-flexible columns:}
\begin{lstlisting}[columns=fullflexible]
std;;mutex mtx;
wwwwwwwwwwwwww;

std::mutex mtx;
\end{lstlisting}

\lstset{
    columns=fixed,
    language=c++,
    extendedchars=true,
    inputencoding=utf8,
    literate={::}{{::}}1
}
\noindent\textit{Fixed columns, :: seen as 1 character:}
\begin{lstlisting}
std;;mutex mtx;
wwwwwwwwwwwwww;

std::mutex mtx;
\end{lstlisting}

\lstset{
    columns=fixed,
    language=c++,
    extendedchars=true,
    inputencoding=utf8,
    basicstyle=\ttfamily,
    literate={::}{{::}}2
}
\noindent\textit{Fixed columns, :: seen as 2 characters, teletype font:}
\begin{lstlisting}
std;;mutex mtx;
wwwwwwwwwwwwww;

std::mutex mtx;
\end{lstlisting}


\end{document}

結果:

ここに画像の説明を入力してください

関連情報