2

Slack the software program, like this website has a very neat feature. When I type backticks like so: `` I am able to write any programmer word in it. Like: someFunction().

How do I do this in LaTeX? I want a similar effect in the output pdf.

I am pretty sure the question is already answered, but I can't think of the right search terms to find it. The scavenger hunt for search terms is real.

  • Are you wanting the word to appear differently when you're editing the source code, or when you're looking at the output pdf? – Teepeemm Feb 19 '18 at 21:28
  • I don't think so. I'm writing a thesis and I want to have a similar style like the backticks give me on StackExchange. I want to recreate that effect in LaTeX. Creating an effect in LaTeX seems very much like a LaTeX related question. – Melvin Roest Feb 19 '18 at 21:28
  • I want it to appear differently in the output pdf. My apologies for not being clear. – Melvin Roest Feb 19 '18 at 21:28
  • 1
    Like this: https://tex.stackexchange.com/questions/19004/how-to-format-an-inline-source-code ? – Paul Gessler Feb 19 '18 at 21:34

2 Answers2

4

Here's a LuaLaTeX-based solution. TeX-special characters, such as _ and &, may occur between pairs of backtick characters. Addendum to incorporate a follow-up comment by the OP: Care needs to be taken in order not to mis-interpret consecutive backtick characters -- those are generally used in TeX and LaTeX documents to initiate some instance of quoted material!

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function backticks2tt ( s )
  return ( s:gsub ( "`(..-)`" , "\\texttt{\\detokenize{%1}}" ) )
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
  "process_input_buffer", backticks2tt, "backticks2tt" )}}
\usepackage{url}

\begin{document}
aa `someFunction()` bb `some_other_Function()` cc
\end{document} 

A comment on the type of pattern matching that's performed by the Lua function backticks2tt seems indicated. The pattern

`(..-)`

is the "non-greedy" way of specifying the pattern "all instances of one or more characters that are encased by backtick characters". To specify a "greedy" pattern match of "one or more characters" in Lua, one would type

`.+`

However, a greedy pattern match wouldn't be appropriate here, as it would end up grabbing the entire string

someFunction()` bb `some_other_Function()

in the MWE shown above and changing it to

\texttt{\detokenize someFunction()` bb `some_other_Function()}

Clearly, that's not right, and that's why it's necessary to specify non-greedy pattern matching.

Mico
  • 506,678
  • 1
    Wait... it's actually possible to do it with actual backticks? That is so cool! I never saw lua code but it looks quite readable :) – Melvin Roest Feb 19 '18 at 21:57
  • @MelvinRoest - This solution works as a preprocessor of sorts: By assigning the backticks2tt function to LuaTeX's process_input_buffer callback, the function's work is performed before "ordinary LaTeX" even starts its usual processing. "Ordinary LaTeX" never actually gets to see and act on {backtick}some_Function(){backtick}; it only gets to see \texttt{\detokenize{some_Function()}}. – Mico Feb 19 '18 at 22:03
  • @MelvinRoest - Lua's string manipulation functions are very powerful. The string.gsub function does pattern matching and substitution. The pattern .- means "any string of 0 or more characters", non-greedy; to get a greedy pattern match -- which wouldn't be correct for the present application! -- one would need to write .*. Encasing the matched pattern in parentheses, like (.-), lets it be referenced as %1 in the second part of gsub. – Mico Feb 19 '18 at 22:08
  • I do have on issue with this which is: how do I type ``some quote''? The `` gets preprocessed. – Melvin Roest Mar 07 '18 at 14:11
  • 1
    Ah found a change that I could make. I read some links on lualatex and the - quantifier of regular expressions in general. And then I realized that one way to force a minimum of one character, non-greedy matching is to add an additional .. So it becomes: "\(..-)`" as opposed to"`(.-)`" ` – Melvin Roest Mar 07 '18 at 14:30
  • 1
    @MelvinRoest - Indeed, my answer failed to consider the rather important case of quoted material, i.e., of strings being encased in pairs of back-quote and forward-quote glyphs. You found the perfect treatment! I'll update my answer accordingly. – Mico Mar 07 '18 at 14:58
  • Hmm, I'm wondering how would you guard against the `some sentence in a single quote' case? – Melvin Roest Mar 09 '18 at 18:23
  • Ah, I can use \lq and \rq – Melvin Roest Mar 09 '18 at 18:31
  • The following question helps to redefine the \lq and \rq to one command:https://tex.stackexchange.com/questions/113327/how-to-do-single-quotes-in-latex-suite – Melvin Roest Mar 09 '18 at 18:41
  • @MelvinRoest - A single sentence (or sentence clause) enclosed in single back- and forward quotes is no problem at all. Recall that the search pattern is for <single backquote>...<single backquote>. I guess if there's more than one clause on a line that's delimited by backquotes and forward quotes, it would be time to switch to a different quoting system. – Mico Mar 09 '18 at 19:20
  • A way to improve the functionality is to use: https://repl.it I am learning a humble thing or two about writing Lua :) – Melvin Roest Mar 11 '18 at 18:29
0

Based on Mico's answer I got a bit more serious. For some reason, I still experienced some issues. So I decided to learn some Lua through some articles and testing it on Repl.it. The website allows me to test and print things quickly.

On Repl.it I iterated on what I wanted until I came to my final design and tested the following code:

function replaceBacktick(input)
  local backtickPattern = "`(..-)`"
  local quotePattern = "`(..-)'"
  isQuoteMatch = input:find(quotePattern)
  if isQuoteMatch == nil then
    print("this is not a quote")
    print( input:gsub(backtickPattern, "\\texttt{" .. input .. "}") )
  else
    print("this is a quote")
  end
end


replaceBacktick( "``the number of correctly identified instances divided by the total number of instances \cite{leong2016} '') and 2.53\% (sensitivity which``")
replaceBacktick("`SomeReactComponent`")

I started working from Mico his answer, because working from my tested Repl.it code resulted in errors and also was a lot more verbose. Still, it helped since I learned how to write if-statements and functions, I didn't know the syntax before.

I made a huge assumption that not triggering the return statement in the if-statement would be fine and would leave text in LaTeX as is (it apparently is). Printing for debugging purposes in LaTeX, even after reading some articles, is a mystery to me.

Anyways, this is the final code that works for me and ignores quotes, even single quotes!

\usepackage{luacode}
\begin{luacode}
function backticks2tt ( input )
    local isQuoteMatch = input:find("`(..-)'")
    if isQuoteMatch == nil then
        return ( input:gsub ( "`(..-)`" , "\\inlinecode{\\detokenize{%1}}" ) )
    end
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
  "process_input_buffer", backticks2tt, "backticks2tt" )}}