4

i have a simple question. I have a regular expression that i want to put into my thesis like a math formula. This is the regular expression:

^var c[12]+Arr = new Array(.*);$

This worked doing it this way:

\[\hat{ }var c[12]+Arr = new Array(.*);\$\]

Now i wanted to change the style from "it" to "sc", because it's not really a formula, trying this:

\[\textsc{\hat{ }var c[12]+Arr = new Array(.*);\$}\]

But now i'm getting this error:

! Missing $ inserted.

How can i solve this little problem? Thx in advance :)

4
  • 2
    Within \textsc{...} LaTeX behaves very much like in normal text mode, so you will have to math-escape \hat like so: \[\textsc{$\hat{\pahntom{x}}$var c[12]+Arr = new Array(.*);\$}\] or even \[\textsc{$\hat{\phantom{x}}$var c[12]+Arr = new Array(.*);\$}\]; it does not look nice though and slightly goes against semantic typesetting, I think, so verbatim or its extensions (the listings package springs to mind) might be better for you.
    – moewe
    Commented Oct 11, 2013 at 8:15
  • That worked thanks, but what do you mean by semantic typesetting? In the description below i format the parameter i'm referencing just like that.
    – Auberotte
    Commented Oct 11, 2013 at 8:22
  • \[...\] are normally used to typeset maths (equations and the like), in the usage I described above \[...\] is only used to center the content (and number it, possibly). The same could be achieved by \begin{center}\textsc{$\hat{\phantom{x}}$var c[12]+Arr = new Array(.*);\$}\end{center}, here we use center to actually describe what we are doing (that's what I meant by "semantic typesetting": using code that describes what you do, and not using some construction that might happen to work but has no semantic connection to what is being done).
    – moewe
    Commented Oct 11, 2013 at 8:30
  • I forgot: With my code above you escape math mode and have to re-enter it via $..$, this is not necessary: replace the \hat{..} construct by \^{}, so you get \[\textsc{\^{}var c[12]+Arr = new Array(.*);\$}\]. But now the sole purpose of [...] is to center the text, and this is certainly bad practice, as \[..\] is for maths. In semantic typesetting you would define a new command such as \newcommand*{\mycode}[1]{\begin{center}\textsc{#1}\end{center}} and use it like so: \mycode{\^{}var c[12]+Arr = new Array(.*);\$} in the document; or use a package that does what you want.
    – moewe
    Commented Oct 11, 2013 at 8:56

2 Answers 2

6

Why math?

Use verbatim and instead.

\documentclass[12pt]{standalone}
\begin{document}
\verb!^var c[12]+Arr = new Array(.*);$!
\end{document}

And the result:

regex

If you want to place it centered:

\documentclass[12pt]{article}
\usepackage[b6paper,landscape]{geometry}
\begin{document}
Text before the regular expression:

\begin{center}
\verb!^var c[12]+Arr = new Array(.*);$!
\end{center}

Text after the regular expression.
\end{document}

Looks like:

centered regex

6
  • I don't like that typewriter style, but the main problem is that it's not centered like with those brackets.
    – Auberotte
    Commented Oct 11, 2013 at 8:14
  • You can change the typewriter style, that's not a problem. And what you mean by "centered"? Position on the page?
    – m0nhawk
    Commented Oct 11, 2013 at 8:20
  • 2
    @Auberotte You are clearly trying to display source code. In my experience the de facto standard for source code is using type writer style. If you want it centered, try \begin{center}\verb!blah!\end{center} or use one of the more sophisticated environments provided by verbatim packages such as listings (lstlisting).
    – moewe
    Commented Oct 11, 2013 at 8:22
  • Exactly, i don't really want it inside my text because it's an important part of my program, so i want to highlight it and descripe it in the section below.
    – Auberotte
    Commented Oct 11, 2013 at 8:24
  • @moewe I have defined listings for my C++-Code but i didn't wanted to format that piece like my Code, anyways you helped me out already, so thanks :)
    – Auberotte
    Commented Oct 11, 2013 at 8:32
8

You could use the listings package, which provides the \lstinline command; it's much more customizable than \verb. In any case I'd use a different typeface for such snippets of code; I repeat the example three times, first with the regular font, then in sans serif type and finally in typewriter type.

\documentclass{article}
\usepackage{textcomp}
\usepackage{listings}
\lstset{columns=fullflexible}
\begin{document}
Text before the regular expression:
\begin{center}
\verb!^var c[12]+Arr = new Array(.*);$!
\end{center}
Text between the regular expressions.
\begin{center}
\lstinline!^var c[12]+Arr = new Array(.*);$!
\end{center}
Text between the regular expressions.
\begin{center}
\lstinline[basicstyle=\sffamily]!^var c[12]+Arr = new Array(.*);$!
\end{center}
Text between the regular expressions.
\begin{center}
\lstinline[basicstyle=\ttfamily]!^var c[12]+Arr = new Array(.*);$!
\end{center}
Text after the regular expression.
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .