3

Instead of using

\usepackage{listings}
...
lstinline{for} or \lstinline{while} or \lstinline{main}
Here you \codeword{see} an \codeword{example} of \codeword{code}

how is it possible to define this syntax for inline-code, similar to Markdown:

Here you `see` an `example` of `code`

?

  • 1
    Backticks are generally best avoided, but look at fancyvrb package or shortvrb package. Both can do what you want, perhaps using | or " as the quote marker. – Thruston Sep 11 '18 at 8:55
  • Duplicate of tex.stackexchange.com/q/361145/15036 ? – Thruston Sep 11 '18 at 8:57
  • I upvoted this question because it raises an interesting debate about why (not) to use backticks to mark code in LaTeX. Well, maybe it doesn't raise it, but the comments and the answer help in deciding how and when to use them. – thymaro Sep 11 '18 at 9:04
3

You could do this with the fancyvrb package. I would suggest using a different separator (like |) to avoid clashes with quotes etc.

\documentclass{article}
\usepackage{fancyvrb}
\DefineShortVerb{\`}
\begin{document}
Here you `see` an `example` of `code`

\end{document}
  • Great, thanks a lot @TeXnician! Icing on the cake: would it be possible to have ~~~~ This is a piece of code in a block ~~~~ or fenced ```` This too ```` as block code (i.e. new paragraph, not inline)? – Basj Sep 11 '18 at 9:29
  • 1
    @Basj Everything with multiple characters is much more difficult (making one character active is easy). Are you sure you don't want to write markdown source and then use pandoc or similar tools to convert that? – TeXnician Sep 11 '18 at 9:38
  • I was initially interested for this solution, but then how do you make chapters, sections, math. etc. inside a document to be converted with Pandoc? I would like to keep `` code `` and ``` code ```, tables, itemization with just * from Markdown, but $math$ and chapters, sections, etc. from LaTeX :) Any idea / example document showing how to use both? – Basj Sep 11 '18 at 9:43
  • @Basj Then probably pandoc is the better solution (see pandoc.org/MANUAL.html#pandocs-markdown which shows how to use headers and math) because also markdown tables and itemization is not that easy. – TeXnician Sep 11 '18 at 9:48
  • Small bug: error with this solution \DefineShortVerb{`} when also using \usepackage{graphicx}: ! Missing number, treated as zero. <to be read again> \let l.30 \chardef\mptopdfstoredatcode\catcode \@ ? ! Emergency stop.. If graphicx` is not loaded, no error... – Basj Sep 11 '18 at 10:12

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