5

I'm writing a document with a ton of pseudocode and it's very tedious to keep wrapping code objects in {\tt text}. It would be so much nicer to, well, do what StackExchange does! There's a one-character symbol, the back-tick, just like for math there's the dollar-sign. Is there any way to accomplish the same thing in LaTeX?

  • 1
    shortverb is your friend – David Carlisle Jun 6 at 19:58
  • If you're using VSCode, use ctrl+l,ctrl+t. – Tvde1 Jun 7 at 10:34
  • Unrelated: don't use two-letter font change commands (\tt, \bf, etc.) in LaTeX. Use the proper more-letter variants (\ttfamily, \bfseries, etc.) – Phelype Oleinik Jun 7 at 19:58
13

enter image description here

\documentclass{article}

\usepackage{shortvrb}
\MakeShortVerb|
\begin{document}

this |and| that or |the other|

\end{document}

shortvrb is in the core base latex distribution.

  • I suspect this is conflicting with the vertical bars that I'm using to declare borders in tabular environments. Is that right, and if so, do you happen to know a fix? – Addem Jun 8 at 18:01
  • 1
    @Addem you can turn it off before the table (\DeleteShortVerb| ) or use any other character that you do not use, such as \MakeShortVerb! or any other character – David Carlisle Jun 8 at 18:04
11

Use fancyvrb and \DefineShortVerb (although you might not want to use `)

\documentclass{article}
\usepackage{fancyvrb}
\DefineShortVerb\`
\begin{document}
I'm writing a document with a ton of pseudocode and it's very tedious to keep wrapping code objects in
`{\tt text}`. It would be so much nicer to, well, do what StackExchange does! There's a one-character
symbol, the back-tick, just like for math there's the dollar-sign. Is there any way to accomplish the same
thing in LaTeX?
\end{document}

enter image description here

  • This might cause problems when you want to use quotation marks. A chacter like | or @ might be more suitable – AJFarmar Jun 7 at 19:46
  • @AJFarmar That's exactly why I said "you might not want to use `` ` ``", I just used it to imitate the markdown syntax (I could've used more emphasis on that warning, though :-). – Phelype Oleinik Jun 7 at 19:56
3

Wat StackExchange does is markdown, so you can also use the markdown package:

\documentclass{article}
\usepackage{markdown}
\begin{document}
\begin{markdown}
This `and` that or `the other` 
\end{markdown}
\end{document}

Or more simple, write a markdown file and export it to LaTeX:

This `and` that or `the other` 

The bonus point of this approach is avoid also many other tedious commands, not only those for verbatim text.

  • I hope you don't mind me adding a link to the package - it was new to me and presumably to others, and looks useful – Chris H Jun 7 at 7:54

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.