Passing preamble to tikzmagic %%tikz in Jupyter notebook - jupyter-notebook

I am using the tikzmagic extension with a Jupyter notebook to embed some TikZ diagrams into the notebook. (I am open to alternatives if there is a better way.)
In one cell, I create an iPython variable preamble like so:
preamble=r'''\tikzset{terminal/.style={
rectangle, minimum size=6mm, rounded corners=3mm, very thick, draw=black!50,
top color=white, bottom color=black!20, font=\ttfamily}}'''
In a subsequent cell, I try to use that variable like this:
%%tikz -f svg -l calc,positioning,shapes.misc -x $preamble
But that ends up generating LaTeX code like
% ⋮
\usetikzlibrary{shapes.misc}
\tikzset{terminal/.style={rectangle,
\begin{document}
% ⋮
It seems to terminate the argument at the ␣ (<space>). If I use
%%tikz -f svg -l calc,positioning,shapes.misc -x "$preamble"
It generates LaTeX code like
% ⋮
\usetikzlibrary{shapes.misc}
"\tikzset{terminal/.style={rectangle, minimum size=6mm, rounded corners=3mm, very thick, draw=black!50,
top color=white, bottom color=black!20, font=\ttfamily}}"
\begin{document}
% ⋮
My apologies if this is the wrong place to ask, but I thought TeX people might have encountered this problem, even though the fault is probably mine or in the Python source.

Related

ZSH vi normal mode to move around printed text

Can I use zsh vi normal mode to move around previous commands output or the printed text in the shell to copy/yank it ?
For example in the screenshot below. I want to move to the output of ls to copy something. When I press j/k zsh cycle my command history but doesn't move up to the printed text. j/k move one line down/up only when I have multiple line command that I'm currently writing but haven't executed yet.
To the best of my knowledge, the ability to access the output of commands interactively is the domain of your terminal (-emulator), not the shell. You would use commands like sed, awk, grep, possibly in a pipe, to access, manipulate and use output you know in advance is the part you are interested in.
To access the output with keyboard shortcuts/command-keys, I suggest using the like of tmux - it allows to copy/yank from the whole terminal display as if it was a text-file in an editor.

only rendering last X lines of chunk output in R Markdown

I am calling a shell program from R Markdown like this
```{sh}
SomeShellProgram -options
```
and render the file as HTML. The calculation the program does take some time, wherefore the author included an self-updating progress "bar" which looks something like this:
45Mb 12.4% 935 OTUs, 3485 chimeras (6.7%)
However, especially if the progress is slow, it will update this line every 0.1% or so. And each line is rendered separately in the HTML, which can ad up to up to 1000 lines of progress bars.
I don't want to suppress the output completely , e.g. with echo=FALSE in the chunk options. I am producing a report and the information that is printed is important.
I am looking for a hack that would somehow only capture the last X lines and render these, or maybe using grep or something similar to only capture the lines that have 100% or so.
I tried redirecting the output with > output.txt but the progress wasn't printed to the file (although other information was).
I can't think of a way to provide a reproducible example without giving the full example, sorry for that.
For those that are interested: I am trying to produce a report on the analysis of 16S Illumina sequencing data and I'm using Usearch and the command that gives me the most headaches is the usearch -cluster_otus command.
UPDATE
There is an additional problem with rendering the last X lines: The progress bar in the output is delimited by ^M(carriage return characters) and not by line breaks, so lessonly recognises it as a single line. Therefore my final solution includes
redirecting the output from the progress bar with 2> into a file
replacing the ^Mcharacters with line breaks using sed
rendering the last X lines with less
My (pseudo)code to do this on mac osx is the following (where X = number of lines)
FunctionWithProgressBar -option 2> tempfile.tmp
sed -ibak $'s/\x0D/\\\n/g' tempfile.tmp
tail -nX tempfile.tmp
and in R Markdown:
```{sh, results="hide"}
FunctionWithProgressBar -option 2> tempfile.tmp
```
```{sh, echo=FALSE}
sed -ibak $'s/\x0D/\\\n/g' tempfile.tmp
tail -nX tempfile.tmp
```
note that matching the backspace is a pain in the butt (especially on osx) and changes between platforms.
The progress bar is probably in the sterr stream, so you capture it with "2>" and not ">" so you could capture stderr and stdout separately, e.g.:
usearch blablabla 2> only_err > only_stdout
Or if you want all of the output together, you have to redirect stderr to stdout, and do an append, as such:
usearch blablabla >> total_output 2>&1
As for the R-markdown part, I cannot really help, never used, sorry.
regards,
Moritz

tmux man-page search highlighting

When I search in, for example, man ls while in a tmux session, the search strings don't appear highlighted - the page jumps down so that the search string is on the top line of the buffer, as expected, but it's not highlighted.
Doing the same thing in the same shell while not in a tmux session results in highlighted search strings.
I have no idea where to start looking to solve this. Any hints are appreciated.
Based on Less Colors For Man Pages by Gen2ly, here is my man page and how to do it:
Preview
This is a shell, not a web page !
How to
(optional) I'm using Tomorrow theme for Konsole/Yakuake ;
Edit your ~/.bashrc ~/.zshrc, etc. to add :
# Colored man pages: http://linuxtidbits.wordpress.com/2009/03/23/less-colors-for-man-pages/
# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;016m\E[48;5;220m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
Reload your config and try a man page search :
. ~/.bashrc && man ls
Fixed it. The problem is to do with the way that the screen $TERM handles italics. From the tmux FAQ:
vim displays reverse video instead of italics, while less displays italics
(or just regular text) instead of reverse. What's wrong?
This matches my problem exactly. The $PAGER used by man is less by default - basically, man uses less to show the contents of the manual pages. In my case, less wasn't highlighting text, just showing regular text.
The reason for this happening:
Screen's terminfo description lacks italics mode and has standout mode in its
place, but using the same escape sequence that urxvt uses for italics. This
means applications (like vim) looking for italics will not find it and might
turn to reverse in its place, while applications (like less) asking for
standout will end up with italics instead of reverse.
The solution is to make a new terminfo file for tmux, which lets it know that italics are supported. The solution's outlined in the (at time of writing) very, very bottom of the tmux FAQ.
After creating the new terminfo file, in tmux: C-b :source-file /absolute/path/to/.tmux.conf (from this SuperUser question) - this should make tmux reload the .tmux.conf file. However, this didn't work for me, and the changes only applied after restarting the tmux server (close all tmux sessions, then re-open them).
This thread is a few years old but is still the one that comes up as the best search result, so I'm answering with what finally worked for me. This is based off of tmux FAQ.
...but the instructions aren't completely clear on when or where to substitute the -256color string. I use gnome-terminal (v 3.16.2) with tmux, and this worked for me:
$ mkdir $HOME/.terminfo/
$ screen_terminfo="screen-256color"
$ infocmp "$screen_terminfo" | sed \
-e 's/^screen[^|]*|[^,]*,/screen-256color|screen with italics support,/' \
-e 's/%?%p1%t;3%/%?%p1%t;7%/' \
-e 's/smso=[^,]*,/smso=\\E[7m,/' \
-e 's/rmso=[^,]*,/rmso=\\E[27m,/' \
-e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo
$ tic /tmp/screen.terminfo
And tell tmux to use it in ~/.tmux.conf:
set -g default-terminal "screen-256color"
Note: I tried it once without the -256color and since that didn't work (still seeing italics instead of highlighting), I had to delete everything under the .terminfo dir (another dir called 's') before the infocmp would work.

Invert PNG graphics file with a non-interactive command

I'm using DruTex, which is a Drupal module to embed Latex equations. The context of the equation is marked, and a Latex interpreter will generate a PNG file:
dvipng -o [IMG_FILE] -D [DPI] -T tight [TMP_DIR]/[HASH].dvi
The dvipng utility is used to generate a PNG from Latex's DVI file. That's good so far, but I could need black background and white foreground for these equations, since otherwise they are hard to read, because they appear in a dark-themed way.
convert (from Imagemagick) doesn't have an -invert flag. Neither does dvipng. So is there a way to invert a png file? I can add something to that routine like:
batch_invert -input [IMG_FILE] -out [IMG_FILE]
However I do not know how I can implement that.
convert has a -negate option that works.
Example:
convert -negate input.png output.png

Piping program output to less does not display beginning of the output

I am trying to make a bunch of files in my directory, but the files are generating ~200 lines of errors, so they fly past my terminal screen too quickly and I have to scroll up to read them.
I'd like to pipe the output that displays on the screen to a pager that will let me read the errors starting at the beginning. But when I try
make | less
less does not display the beginning of the output - it displays the end of the output that's usually piped to the screen, and then tells me the output is 1 line long. When I try typing Gg, the only line on the screen is the line of the makefile that executed, and the regular screen output disappears.
Am I using less incorrectly? I haven't really ever used it before, and I'm having similar problems with something like, sh myscript.sh | less where it doesn't immediately display the beginning of the output file.
The errors from make appear on the standard error stream (stderr in C), which is not redirected by normal pipes. If you want to have it redirected to less as well, you need either make |& less (csh, etc.) or make 2>&1 | less (sh, bash, etc.).
Error output is sent to a slightly different place which isn't caught by normal pipelines, since you often want to see errors but not have them intermixed with data you're going to process further. For things like this you use a redirection:
$ make 2>&1 | less
In bash and zsh (and csh/tcsh, which is where they borrowed it from) this can be shortened to
$ make |& less
With things like make which are prone to produce lots of errors I will want to inspect later, I generally capture the output to a file and then less that file later:
$ make |& tee make.log
$ less make.log

Resources