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`

?

Basj
  • 764
  • 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 08:55
  • Duplicate of https://tex.stackexchange.com/q/361145/15036 ? – Thruston Sep 11 '18 at 08: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 09:04

1 Answers1

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}
TeXnician
  • 33,589
  • 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 09: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 09: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 09:43
  • @Basj Then probably pandoc is the better solution (see https://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 09:48
  • Small bug: error with this solution \DefineShortVerb{\}when also using\usepackage{graphicx}:! Missing number, treated as zero. \let l.30 \chardef\mptopdfstoredatcode\catcode` \@ ? ! Emergency stop.`. If `graphicx` is not loaded, no error... – Basj Sep 11 '18 at 10:12
  • @Basj I did mention that it is not a bug, but your choice of characters. Try moving \DefineShortVerb as far to the document begin as possible. is an essential character for TeX, so you do break things and cause clashes by making it active for some verbatim environment. That is why the LaTeX kernel and others prefer characters like|`. – TeXnician Sep 11 '18 at 10:14