Skip to content

Commit

Permalink
LaTeX writer: use "\phantom{\ }" to escape spaces in texttt
Browse files Browse the repository at this point in the history
This approach seems more robust, and solve the issue of normal escaped spaces not working after a forced line break inside texttt command.
  • Loading branch information
timtylin committed Feb 18, 2015
1 parent d9403ad commit 704dbaa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ inlineToLaTeX (Code (_,classes,_) str) = do
rawCode = liftM (text . (\s -> "\\texttt{" ++ escapeSpaces s ++ "}"))
$ stringToLaTeX CodeString str
where
escapeSpaces = concatMap (\c -> if c == ' ' then "\\ " else [c])
escapeSpaces = concatMap (\c -> if c == ' ' then "\\phantom{\\ }" else [c])
inlineToLaTeX (Quoted qt lst) = do
contents <- inlineListToLaTeX lst
csquotes <- liftM stCsquotes get
Expand Down
13 changes: 7 additions & 6 deletions tests/scholdoc.latex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ support}\label{scholarlymarkdown-math-support}
\subsection{Math as (fenced) code
blocks}\label{math-as-fenced-code-blocks}

This is a line of text with a \texttt{simple\ code\ block} in it.
This is a line of text with a
\texttt{simple\phantom{\ }code\phantom{\ }block} in it.

\texttt{`this\ should\ be\ just\ a\ `normal`\ inline\ code\ block\ surrounded\ by\ literal\ backticks`}
\texttt{`this\phantom{\ }should\phantom{\ }be\phantom{\ }just\phantom{\ }a\phantom{\ }`normal`\phantom{\ }inline\phantom{\ }code\phantom{\ }block\phantom{\ }surrounded\phantom{\ }by\phantom{\ }literal\phantom{\ }backticks`}

This is another line of text. Here should be some math:
$\mathbf{F = ma}<2\mathbf{ma}$. There should be some displaymath
Expand Down Expand Up @@ -131,8 +132,8 @@ Here's a list with both inline and display math environments:
\item
Item 1 is a famous item
\item
Item 2 with a \texttt{code\ block} and $\mathsf{\text{inline math}}$
with equation $\mathbf{y=Ax}$
Item 2 with a \texttt{code\phantom{\ }block} and
$\mathsf{\text{inline math}}$ with equation $\mathbf{y=Ax}$
\item
Item 3

Expand Down Expand Up @@ -265,9 +266,9 @@ Here are some algorithms using various methods. The ``most canonical
one'' is currently just using a line-block:

\begin{scholmdAlgorithm}
\texttt{\ 1.}~\textbf{Inputs}:~variables~$A, b$\\\texttt{\ 2.}~\textbf{Output}:~$\phi$~~~~~~~~~~~~~~~~\texttt{//this\ is\ a\ comment}
\texttt{\phantom{\ }1.}~\textbf{Inputs}:~variables~$A, b$\\\texttt{\phantom{\ }2.}~\textbf{Output}:~$\phi$~~~~~~~~~~~~~~~~\texttt{//this\phantom{\ }is\phantom{\ }a\phantom{\ }comment}

\texttt{\ 3.}~Choose~an~initial~guess~$\phi$~to~the~solution\\\texttt{\ 4.}~~\textbf{repeat}~until~convergence\\\texttt{\ 5.}~~~~\textbf{for}~$i$~\textbf{from}~1~\textbf{until}~$n$~\textbf{do}\\\texttt{\ 6.}~~~~~~~~$\sigma \leftarrow 0$\\\texttt{\ 7.}~~~~~~~~\textbf{for}~$j$~\textbf{from}~1~\textbf{until}~$n$~\textbf{do}\\\texttt{\ 8.}~~~~~~~~~~~~\textbf{if}~$j \ne i$~\textbf{then}\\\texttt{\ 9.}~~~~~~~~~~~~~~~$\sigma \leftarrow \sigma + a_{ij} \phi_j$\\\texttt{10.}~~~~~~~~~~~~\textbf{end~if}\\\texttt{11.}~~~~~~~~\textbf{end}~($j$-loop)\\\texttt{12.}~~~~~~~~$\phi_i \leftarrow \frac 1 {a_{ii}} (b_i - \sigma)$\\\texttt{13.}~~~~\textbf{end}~($i$-loop)\\\texttt{14.}~~~~check~if~convergence~is~reached\\\texttt{15.}~\textbf{end}~(repeat)
\texttt{\phantom{\ }3.}~Choose~an~initial~guess~$\phi$~to~the~solution\\\texttt{\phantom{\ }4.}~~\textbf{repeat}~until~convergence\\\texttt{\phantom{\ }5.}~~~~\textbf{for}~$i$~\textbf{from}~1~\textbf{until}~$n$~\textbf{do}\\\texttt{\phantom{\ }6.}~~~~~~~~$\sigma \leftarrow 0$\\\texttt{\phantom{\ }7.}~~~~~~~~\textbf{for}~$j$~\textbf{from}~1~\textbf{until}~$n$~\textbf{do}\\\texttt{\phantom{\ }8.}~~~~~~~~~~~~\textbf{if}~$j \ne i$~\textbf{then}\\\texttt{\phantom{\ }9.}~~~~~~~~~~~~~~~$\sigma \leftarrow \sigma + a_{ij} \phi_j$\\\texttt{10.}~~~~~~~~~~~~\textbf{end~if}\\\texttt{11.}~~~~~~~~\textbf{end}~($j$-loop)\\\texttt{12.}~~~~~~~~$\phi_i \leftarrow \frac 1 {a_{ii}} (b_i - \sigma)$\\\texttt{13.}~~~~\textbf{end}~($i$-loop)\\\texttt{14.}~~~~check~if~convergence~is~reached\\\texttt{15.}~\textbf{end}~(repeat)
\caption{caption for this algorithm}\label{alg:gs}
\end{scholmdAlgorithm}

Expand Down
3 changes: 2 additions & 1 deletion tests/writer.latex
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ These shouldn't be math:
\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
To get the famous equation, write \texttt{\$e\ =\ mc\^{}2\$}.
To get the famous equation, write
\texttt{\$e\phantom{\ }=\phantom{\ }mc\^{}2\$}.
\item
\$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if ``lot'' is
emphasized.)
Expand Down

0 comments on commit 704dbaa

Please sign in to comment.