Environment where all formatting is ignored

99封情书 提交于 2019-12-11 10:05:31

问题


Does there exist a command in LaTeX, such that all that I write within that environment is showed as it is?

For instance when I want to make a guide on how to use commands in LaTeX I'm interested in writing something like:

In order to write $\cap$, you use the command \cap.

But this will return an error because (the last) \cap isn't in the correct environment. So what can I do to make LaTeX ignore the command and just show the text?

I have tried with \mbox{\cap}, which does not work, I am also aware that I can do $\backslash$cap, but this is quite a problem specific solution.


回答1:


You can either use the default LaTeX \verb (with a choice of paired delimiters) or listings (which supports bracketed arguments using \lstinline):

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}

In order to write~$\cap$, you use the command~\verb|\cap|.

In order to write~$\cap$, you use the command~\lstinline{\cap}.

\end{document}

There are other options as well, but this seems to suit your needs. Of course, if you want to use something like \function instead of \lstinline, then add

\let\function\lstinline

after loading the listings package.




回答2:


Try the following:

\documentclass[english]{article}
%\usepackage{verbatim}

\begin{document}

In order to write \verb|$\cap$|, you use the command \texttt{\textbackslash cap}.

\end{document}

Notice that, to use inline verb, you don't even need the package verbatim.

Furthermore, \textbackslash must be used here, instead of $\backslash$.

Addendum:

Reading your comment below, I think something like the following may be useful:

\documentclass[english]{article}

\newcommand{\mycomm}[1]{\texttt{\textbackslash #1}}

\begin{document}

In order to write $\cap$, you use the command \mycomm{cap}.

\end{document}


来源:https://stackoverflow.com/questions/31650471/environment-where-all-formatting-is-ignored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!