Here, all code is shown:
![enter image description here](https://i.stack.imgur.com/DyovK.png)
Here, some code is hidden:
![enter image description here](https://i.stack.imgur.com/00YU1.png)
Here are some implementation details.
Invoke the "ghosts" of Martin Scharrer and Donald Arseneau
The \confidentiel
macro defined below is based on a modified version of Martin Scharrer's \UL@putbox
, which is itself a "phantom" version of a macro defined in Donald Arseneau's ulem
package.
Such a macro applies cleanly over multiple lines, both inside and outside listings, and will prevent your readers from copying & pasting the hidden parts, no matter which PDF viewer they use.
Indulge yourself
Instead of escaping to LaTeX only to invoke your \confidentiel
macro, the following approach defines delimiters that apply \confidentiel
to what they delimit.
The backtick character, `
, is an adequate (opening and closing) delimiter here, because it's unlikely to occur in Matlab code (it's only valid in Matlab string literals and system commands, nowhere else).
The main benefits of this approach is that hiding/unveiling stuff, either locally or globally, is fast and easy.
- Hiding/unveiling stuff locally only requires adding/removing two backticks (
`...`
), as opposed to adding/removing \confidentiel{...}
each time;
- Hiding/unveiling stuff globally can be done by at the flip of the
\ifconfid
switch (comment/uncomment the line \confidtrue
).
Use matlab-prettifier
I recommend using the matlab-prettifier
package instead of mcode
... but, as the author of the former, I'm somewhat biased :p
Have a look at this answer, and see if you like matlab-prettifier
enough to migrate from mcode
.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{matlab-prettifier}
\usepackage[normalem]{ulem}
\newif\ifconfid
\confidtrue % comment this line out to unveil all hidden text
\colorlet{confidcolor}{black} % change the colour of boxes here (e.g. "red")
% ----------- implementation details -----------
\makeatletter
% from Martin's Scharrer's answer (https://tex.stackexchange.com/a/16004/21891)
\def\confid@UL@putbox{%
\ifx\UL@start\@empty%
\else % not inner
\vrule\@width\z@ \LA@penalty\@M
{\UL@skip\wd\UL@box \UL@leaders \kern-\UL@skip}%
\phantom{\box\UL@box}%
\fi
}
\newcommand\confidentiel{}%
\ifconfid
\renewcommand\confidentiel[1][confidcolor]{%
\bgroup%
\let\UL@putbox\confid@UL@putbox%
\markoverwith{\hbox to.01em{\hss\textcolor{#1}{|}\hss}}\ULon%
}
\fi
\lst@AddToHook{Init}{%
\ifconfid%
\lstset{moredelim=[is][\confidentiel]``,keepspaces}%
\else
\lstset{moredelim=**[is][]``}%
\fi
}
\makeatother
% ----------- end of implementation details -----------
\lstset{
style=Matlab-editor,
basicstyle=\mlttfamily,
}
\begin{document}
\confidentiel{%
this text is hidden\\
this text is hidden\\
this text is hidden\\
}%
\xout{test that \textsf{ulem} works}
\begin{lstlisting}
for i=1:3
a=b;
% Guess what's next...
`b=c;`
c=d;
end
`% this text is hidden
% this text is hidden
% this text is hidden
`
\end{lstlisting}
\end{document}
You could use the \captionof
command from the package caption
:
% arara: pdflatex
\documentclass{article}
\usepackage{listings}
\usepackage{caption}[2015/09/20]
\usepackage{showframe} % just for demo
\begin{document}
\begin{center}
\begin{minipage}[t]{.43\textwidth}
\begin{lstlisting}
// Code
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{.54\textwidth}
\begin{lstlisting}
// Code
\end{lstlisting}
\end{minipage}
\captionof{lstlisting}{caption}
\end{center}
\end{document}
![enter image description here](https://i.stack.imgur.com/hq8Ds.png)
Best Answer
If what you are looking for is to display an image with its corresponding source code, then the showexpl package might be what you are looking for.
But since I suspect you just want to use any picture next to some code, then I sugest using the
minipage
environment. For example:which yields the following output:
Of course, you can then adjust the parameters of the
minipage
as you see fit, but I find this basic structure to be sufficient.Note that you can't put a float inside the minipage environment. For more information, see How to use figure inside a minipage?
Hope it helps!