Hot answers tagged

100

Normally a monospaced font is used for this. This is accomplished with \texttt{...}. If you want to use code, you can use \def\code#1{\texttt{#1}}. From that point on you can write \code{...} to get monospaced output.


59

latexindent.pl is available on ctan and is part of texlive See the documentation in pdf or html for full details. You can find (and follow) the project on github at https://github.com/cmhughes/latexindent.pl basic usage You can run latexindent from the command line by typing one of the following, depending on your operating system and tex ...


52

Since LaTeX is meant primarily intended for printed text I would strongly recommend you to not use e.g. grey background, but just use a typewriter font, e.g. The class \texttt{List.class} is \ldots You can and should wrap this in a new command to be free to change the visual appearance later, e.g. \newcommand{\code}[1]{\texttt{#1}} so you can use a form ...


30

algorithmicx You can define your own keywords when using the algorithmicx package. Section 3.1.10 Changing command names of the package documentation discusses the use of \algnewcommand/\algrenewcommand that you can use to create a new command/change existing formatting. The following two images are taken directly from the package documentation: ...


29

I can give you a couple of tips for packages and tools to start experimenting with, I tried several tools in the past: I like the verbatim and verbatim* environments in TeX. It is an easy and a straighforward way of typesetting a small portion of code right away. However, it cannot wrap lines. I was using the fancyvrb package some time ago, http://ctan.org/...


28

Update: I am still improving the code style. You can find the latest version here: https://www.writelatex.com/74567mmxwkw - I appreciate anyone who is testing :) If you want to get a copy of the source code, you can get it here: https://dl.dropboxusercontent.com/u/74217418/stackexchange/tex/html5_listings_sample.tex After much trial and error and the help ...


28

Taken partially from my old answer here (click). This code should not be considered as a bombastic template but an easy-to-customize one. \documentclass[dvipsnames,border=15pt,preview,12pt]{standalone} \usepackage{xcolor} \usepackage{listings} \usepackage{accsupp} \newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}} \lstdefinestyle{...


27

Below are two different math fonts that may assist you in what you want: \documentclass{article} \usepackage{amsmath,amssymb} \begin{document} $\mathcal{H}\quad\mathfrak{H}$ \end{document} See the The Comprehensive LATEX Symbol List under Math Alphabets. An alternative source for all TeX and LaTeX package information and general documentation is texdoc....


24

The suggested and accepted answer does not work correctly as the \captionof command requires to be inside an environment. This is pointed out in the caption documentation. For example in my case there was no proper vertical space after the caption. A proper solution is to define a new environment: \usepackage{caption} % ... \newenvironment{code}{\...


18

listings packages will help you. Using style=A Using style=B TeX Input File \documentclass[dvipsnames,cmyk]{article} \usepackage{listings,xcolor} \lstset { breaklines=true, tabsize=3, showstringspaces=false } \lstdefinestyle{Common} { extendedchars=\true, language={[Visual]Basic}, frame=single, %==============================...


18

If you want a single word to look like a coded word and also to have a light-gray background as in StackExchange you can predefine a color \definecolor{light-gray}{gray}{0.95} and then define a new command: \newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}. From this point on you can use \code{word} to get mono-spaced words with gray background. ...


18

Not contradicting anything in JoG's answer: \documentclass{article} \usepackage{xcolor} \usepackage{listings} \lstset{basicstyle=\ttfamily, showstringspaces=false, commentstyle=\color{red}, keywordstyle=\color{blue} } \begin{document} \begin{lstlisting}[language=Java,caption={Java version}] public class HelloWorld { // Here's the main class ...


18

verbatim is a very special environment which looks for the exact string \end{verbatim} to end it. Unlike other latex environments you can't use \env ...\endenv instead of \begin{env} ...\end{env} with verbatim. If you really want to start with \verbatim then you must do something like this to get around the errors due to the various latex settings for ...


17

The conversion of two consecutive newlines to a \par token comes from TeX's parser and so all formats of TeX (plain, LaTeX, ConTeXt) observe it too. Note that \par gobbles all whitespace until the next non-whitespace token, so three or more consecutive newlines have the same effect as two: one new paragraph. So if you want to have three blank lines ...


16

Package verbatim supports the creation of own verbatim environment, see section 2.1 of the package documentation. The following example defines environment metaverbatim in order to allow \begin{verbatim} and \end{verbatim} inside the verbatim block: \documentclass{article} \usepackage{verbatim} \newenvironment{metaverbatim}{\verbatim}{\endverbatim} \begin{...


16

You can emulate it with a property list: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\DefineDictionary}{mm} { \arclupus_dict_def:nn { #1 } { #2 } } \seq_new:N \l__arclupus_dict_temp_seq \cs_new_protected:Nn \arclupus_dict_def:nn { \prop_gclear_new:c { g_arclupus_#1_dict_prop } \clist_map_inline:nn { #2 } { ...


15

You can also use the relative new package upquote, which is explicitly created for that task – together with the change for single quote marks, compare How to make a real apostrophe or single-quote in LaTeX. \documentclass{article} \usepackage{upquote} \begin{document} \begin{verbatim} Example: `2 + 2` \end{verbatim} \end{document} Without upquote: With ...


15

The syntax for \verb is \verb<char><text><char> where <char> should be a (non special) character not found in <text>. Most often | or + are used for <char>. A verbatim environment doesn't make sense in a table cell belonging to a column declared as l, c or r, just like a quote environment doesn't make sense in the ...


15

This is also possible using the minted package and the highlighting tool pygmentize The command for minted inline code is called \mintinline{language}{code} The inline highlighting will only colorize keywords, that are defined for the language and colored properly according to the highlighting style you choose. \documentclass{scrartcl} \usepackage{minted}...


15

This answer tries to reproduce the screenshot. It uses the lcd package, which defines an extended alphanumeric character set, with each character constructed using dots set in a 5 x 7 matrix. Thus, characters are defined by a binary sequence of 35 bits to signify whether a cell has a visible dot, or is empty. The screen shot shows two character sizes, with ...


14

Lyx's "Insert -> Program Listing" is internally based on the listings package, which provides a a plenty full of options to influence the typesetting. You can enter such listingsoptions on the "Advanced" page of the "Listing Settings" dialog. For instance, entering keywordstyle={\color{blue}} there would typeset all keywords in blue color. For a quick ...


14

You have to set the language and the kind of highlighting, e.g. \documentclass{article} \usepackage[T1]{fontenc} \usepackage{xcolor} \usepackage{lmodern} \usepackage{listings} \lstset{language=[90]Fortran, basicstyle=\ttfamily, keywordstyle=\color{red}, commentstyle=\color{green}, morecomment=[l]{!\ }% Comment only with space after ! } \begin{...


14

UPDATE Martin Scharrer's lstautogobble package takes care of the tabs issue using the listings package; for the other issues, you can and the mathescape=true, and escapeinside options; a little example: \documentclass{article} \usepackage{listings} \usepackage{lstautogobble} \lstset{basicstyle=\ttfamily, mathescape=true, escapeinside=||, autogobble}...


14

Make it this way: word% \footnote{footnote text} more text In other words set a percent sign directly after the word, where the footnote belongs to. See also What is the use of percent signs (%) at the end of lines?.


14

Using any one of algorithm2e, algorithmic or algorithmicx would allow you to typeset pseudocode with your own definitions for variables and keywords. Here is a small example using algpseudocode (provided by algorithmicx): \documentclass{article} \usepackage{algorithm}% http://ctan.org/pkg/algorithms \usepackage{algpseudocode}% http://ctan.org/pkg/...


14

The listings package has a command \lstinline{snippet} that is used exactly for that. You can configure its appearance (including syntax highlighting) using the package options.


14

Nevermind, I found a way to do it. I was using the builtin code highlighter. Setting the --listings flag, I can switch to the lstlistings package and use the following syntax: ~~~~{caption="The preprocessing step" label=lstpreprocess} def myfunction(var): """ Oh how awesome this is. """ pass ~~~~ Pandoc is awesome.


14

You can even use the Mathematica Output: \documentclass{article} \usepackage{xparse,xcolor} \ExplSyntaxOn \NewDocumentCommand{\pythtriples}{m} { \begin{flushleft} \setlength{\fboxsep}{0pt} % \colorbox doesn't add to the width \setlength{\lineskiplimit}{\maxdimen} % all lines are too near \setlength{\lineskip}{1pt} % it's the default, but makes no ...


Only top voted, non community-wiki answers of a minimum length are eligible