2

Scenario

I want to write a tutorial on PSTricks, but because latex-dvips-ps2pdf (see the code shown in First Attempt section) makes my effort to prevent reader from copying the listing numbers no longer work

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

and I cannot use microtype and the output is a bit strange as follow

enter image description here

As a result, this problem forced me to use pdflatex (see Second Attempt section).

First Attempt (latex-dvips-ps2pdf)

\documentclass[dvipsnames,border=12pt,preview]{standalone}

\usepackage{pstricks}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\makeatletter
\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}

\lstdefinestyle{PSTricks}
{
    language={[LaTeX]TeX},
    alsolanguage={PSTricks},
    basicstyle=\small\ttfamily\null,
    keywordstyle=\color{blue},
    backgroundcolor=\color{yellow!10},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{Red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,% expands outward, cannot affect if frame=none
    framerule=\fboxrule,% expands outward, cannot affect if frame=none
    rulecolor=\color{red},% cannot affect if frame=none
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    preset={\setlength{\parindent}{\parindent@save}\centering},
}
\makeatother


\begin{document}
\LTXexample[pos=t,style=PSTricks]
\begin{pspicture}[showgrid](3,3)
    \pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\endLTXexample
\end{document}

Second Attempt (pdflatex)

Using pdflatex can prevent users from copying the listing numbers. However, preset=\centering is no longer work when I use graphic.

\documentclass[dvipsnames,border=12pt,preview]{standalone}

\usepackage{xcolor}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\makeatletter
\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}

\lstdefinestyle{PSTricks}
{
    language={[LaTeX]TeX},
    alsolanguage={PSTricks},
    basicstyle=\small\ttfamily\null,
    keywordstyle=\color{blue},
    backgroundcolor=\color{yellow!10},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{Red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,% expands outward, cannot affect if frame=none
    framerule=\fboxrule,% expands outward, cannot affect if frame=none
    rulecolor=\color{red},% cannot affect if frame=none
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    preset={\setlength{\parindent}{\parindent@save}\centering},
}


\makeatother


\usepackage{filecontents}
\begin{filecontents*}{diagram.tex}
\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}[showgrid](3,3)
    \pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\end{document}
\end{filecontents*}

\immediate\write18{latex diagram.tex && dvips diagram.dvi && ps2pdf  diagram.ps}


\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}
\end{document}

Original Output: enter image description here

Expected Output: enter image description here

Question

How to center the graphic of \LTXexample for the code shown in the Second Attempt section?

2 Answers2

2

try

...
\makeatletter
\let\SX@I\SX@resultInput
\def\SX@resultInput{\centerline{\SX@I}}
\makeatother

\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}
\end{document}
  • I will be better if this setting does not affect other instances of LTXexample and LTXinputExample with no graphic option used. – kiss my armpit Jul 26 '14 at 05:01
2

Taken from Herbert's another answer (click) several decades ago and I modified to meet my requirement. But honestly I am not sure whether or not it is safe for long-term use.

\documentclass[dvipsnames,border=12pt,preview]{standalone}

\usepackage{xcolor}
\usepackage{showexpl}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\makeatletter

\renewcommand*\SX@resultInput{%
  \ifx\SX@graphicname\@empty
    \begingroup
      \MakePercentComment\makeatother\catcode`\^^M=5\relax
      \SX@@preset\SX@preset
      \if@SX@rangeaccept
       \let\SX@tempa=\SX@input
      \else
       \let\SX@tempa=\input
      \fi
      \SX@tempa{\SX@codefile}\par%
    \endgroup
  \else
  \sbox\SX@tempbox{\includegraphics{\SX@graphicname}}
    \begin{center}
      \expandafter\includegraphics\expandafter[\SX@graphicparam]{\SX@graphicname}
    \end{center}
  \fi
}


\newlength{\parindent@save}
\setlength{\parindent@save}{\parindent}

\lstdefinestyle{PSTricks}
{
    language={[LaTeX]TeX},
    alsolanguage={PSTricks},
    basicstyle=\small\ttfamily\null,
    keywordstyle=\color{blue},
    backgroundcolor=\color{yellow!10},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{Red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,% expands outward, cannot affect if frame=none
    framerule=\fboxrule,% expands outward, cannot affect if frame=none
    rulecolor=\color{red},% cannot affect if frame=none
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    preset={\setlength{\parindent}{\parindent@save}},
}


\makeatother


\usepackage{filecontents}
\begin{filecontents*}{diagram.tex}
\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}[showgrid](3,3)
    \pscircle[linecolor=red](1.5,1.5){1.5}
\end{pspicture}
\end{document}
\end{filecontents*}

\immediate\write18{latex diagram.tex && dvips diagram.dvi && ps2pdf  diagram.ps}


\begin{document}
\LTXinputExample[pos=t,style=PSTricks,graphic={[scale=1]"diagram"}]{diagram.tex}

\LTXexample[pos=b]
\LaTeX\ is fun!
\endLTXexample
\end{document}

enter image description here