3

How could I write a command in LaTeX, so after compiling it will be shown as a text in PDF and not executed?

For Example, I would write \it This is how you write in italic and the compiled pdf would contain the exact same text between the quotation marks without \it being executed.

  • 1
    You can use the verbatim environment if you have a lot of text or you can use \verb|\it|. – Sigur Jan 24 '14 at 13:21
  • 12
    \verb+\it+ and NO!!!!! you do NOT use \it to get italic!!! Please to not teach this to users. Use \textit{...} or if it is for emphasizing \emph{...}. \it is TeX and should not be used in LaTeX – daleif Jan 24 '14 at 13:23
  • 1
    \itshape is also acceptable as a switch. – Steven B. Segletes Jan 24 '14 at 13:27
  • Sorry, I don't usually use \it, it was just the first thing that came to my mind when I was thinking about a simple example – user44697 Jan 24 '14 at 13:50
9

You can put them in the verbatim environment.

\begin{verbatim}
\it This is how you write in italic
\end{verbatim}

P.S. If you need to put them in-line, you can put them in the \verb== environment:

\verb=\it This is how you write in italic=
  • @Sigur According to here: the verbatim environment is supported directly by LaTeX but by loading the verbatim package one gets a better implementation. – herohuyongtao Jan 24 '14 at 13:36
  • 2
    It would be nice if you could explain how the command works (as opposed to how it works with = and = as delimiters. – user10274 Jan 24 '14 at 14:58
1

showexpl package that loads listings package might be interesting for you as it provides more sophisticated features as follows.

\documentclass[preview,border=12pt,12pt]{standalone}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{showexpl}


\lstdefinestyle{Common}
{   
    language={[LaTeX]TeX},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=2,
    columns=flexible,
    includerangemarker=false,
    rangeprefix=//\ ,
}


\lstdefinestyle{A}
{
    style=Common,
    backgroundcolor=\color{yellow!10},
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color{blue}\bf,
    identifierstyle=\color{black},
    stringstyle=\color{red},
    commentstyle=\color{green}
}

\usepackage{graphicx}
\begin{document}
\section*{lstinline}
\lstinline[style=A]|\LaTeX\ is fun.|

\section*{lstlisting}
\begin{lstlisting}[style=A]
\LaTeX\ is also fun in displayed mode.
\end{lstlisting}

\section*{LTXexample}
\LTXexample[style=A]
\LaTeX\ is fun.
\endLTXexample
\end{document}

enter image description here

0

The ltxdoc document class, which is used for LaTeX documentation, contains the following definition:

\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}

With this definition you can use \cs{textit} and the output will contain \textit.

  • With this command is difficulty to produce \textit{}. Compare \cs{textit{}} and \cs{textit\{\}} with \verb|\textit{}| – Sigur Jan 24 '14 at 13:33
  • 2
    @Sigur ltxdoc provides also macros \marg and \oarg for mandatory and optional arguments. You'd write something like \cs{textit}\marg{italic text} – cgnieder Jan 24 '14 at 14:00
  • 2
    i've got a macro in my faq.sty that allows me to say \cmdinvoke{providecommand}[n]{command def} (and a variety of things involving *s and the like. recommended only for readers with a tex-like stomach... http://mirror.ctan.org/help/uk-tex-faq/faq.sty – wasteofspace Jan 24 '14 at 14:44
0

If you want to accomplish this inside a section title, it doesn't work with \verb|\it| AFAIK. However, instead, you could use \textbackslash it\ { \ }. to get \it{} printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.