I don't know how to display images in the terminal.
Here is a .rkt test script:
#lang racket
(require 2htdp/image)
(circle 10 "solid" "red")
Then, I do this in the terminal:
$ racket
Welcome to Racket v6.8.
> (enter! "test.rkt")
(object:image% ...)
"test.rkt">
So I'm getting (object:image% ...) when I should be getting an image of a circle.
How can I get images to display in the terminal?
I stumbled on this question recently as well.
Tried many combinations of keywords searching online and finally found a working solution.
For example,
; this approach would NOT render the image
; start repl inside a terminal
$ racket -i
(require 2htdp/image)
(circle 5 "solid" "red")
; this approach would render the image in a canvas
; start repl inside a terminal
$ racket -i
(require 2htdp/image racket/gui/base (only-in pict show-pict))
(show-pict (circle 5 "solid" "red"))
Regarding the comment by #Alexis King, as far as I know, although iTerm2 could display image, it would not work inside a repl. I could be wrong though and would be happy to know if there are alternatives out there.
I can finally use vim + tmux to play with racket now!
Related
Working through SICP using Emacs, Geiser, and MIT Scheme, I decided to switch over to Racket in order to properly do the exercises in section 2.2.4 involving the Picture Language.
Configuration
I got a setup together that works for MIT Scheme and Racket using the following ~/.emacs configuration:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(setq geiser-mit-binary "/usr/bin/scheme")
(setq geiser-racket-binary "/usr/bin/racket")
(setq geiser-active-implementations '(mit racket))
(add-to-list 'auto-mode-alist '("\\.rkt\\'" . geiser-mode))
Workflow with MIT Scheme
Here's my workflow for working with MIT Scheme:
First, I open a file (fib.scm) using emacs fib.scm. I'm asked to pick the Scheme implementation, which I answer with mit.
Second, I write my fib function, press C-c C-z to open a REPL.
Third, I switch back to the code buffer with C-x o and evaluate it using C-c C-b.
Fourth, I switch to the REPL buffer with C-x o and evaluate some expressions.
This it how it looks like:
Workflow with Racket
Here's my (intended) workflow for working with the SICP language of Racket:
First, I open a file (fib.rkt) using emacs fib.rkt. Interestingly, I'm not asked to pick a Scheme implementation.
Second, I write my fib function, but use #lang sicp in the first line. Then I open the REPL using C-c C-z.
Third, I switch back to the code buffer with C-x o and evaluate it using C-c C-b, which gives no error message, but prints => #<void> at the very bottom.
Fourth, I switch back to the REPL buffer with C-x o, where I fail to evaluate an expression like (fib 3):
2 racket#> (fib 3)
3 fib: undefined;
4 cannot reference an identifier before its definition
5 in module: top-level
6 context...:
7 body of top-level
8 /usr/share/racket/collects/racket/repl.rkt:11:26
This it how it looks like:
What am I doing wrong? Is it the configuration, is it the way I use it?
Example with sicp-pict
When I run the following code using the Racket workflow from above, the picture of Einstein is properly displayed:
#lang sicp
(#%require sicp-pict)
(paint einstein)
So my setup is not entirely broken…
Addendum
Having re-installed the geiser-racket package, I'm now able to start a REPL. I'm also able to evaluate the entire buffer using C-c C-b, which prints => #<void> to the bottom of the screen.
However, when I try to actually invoke a function, I get this error:
1 Welcome to Racket v8.6 [cs].
2 racket#> (fib 3)
3 +: contract violation
4 expected: number?
5 given: #<procedure:fib>
6 context...:
7 /usr/share/racket/collects/racket/private/norm-define.rkt:52:83: body of top-level
8 /usr/share/racket/collects/racket/repl.rkt:11:26
Edit: Oh dear, it was a simple syntax error... my bad! Everything is fine now!
To finish things up, I just post the steps I have taken to get everything to run:
Use melpa instaed of melpa-stable (~/emacs):
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)
Refresh the packages after re-opening Emacs:
M-x package-refresh-contents
Install racket-mode:
M-x package-install RET racket-mode
Install the package geiser-racket:
M-x package-install RET geiser-racket
Extend your Geiser config (~/emacs):
(setq geiser-mit-binary "/usr/bin/scheme")
(setq geiser-racket-binary "/usr/bin/racket")
(setq geiser-active-implementations '(mit racket))
Start using the picture language:
#lang sicp
(#%require sicp-pict)
(paint einstein)
Open it in Emacs:
emacs ../examples/einstein.rkt
Start a Racket REPL and evaluate the whole buffer:
C-c C-z
C-x o
C-c C-b
An image of Einstein should appear.
Am getting the following error when trying to run (require 2htdp/image) in DrRacket:
module: identifier already required
The error occurs after updating from DrRacket 7.3 to 7.4.
I uninstalled all previous versions of DrRacket including 7.4 and reinstalled 7.3. Am continuing to get error.
I went through some of the solutions in stackoverflow for this error message before posting. However, my code is simple. I cannot even run (require 2htdp/image) on its own without code without triggering the error.
[edit]
Check-syntax:
unsaved-editor:2:9: module: identifier already required in: rectangle
I have no idea what that means, since there is no rectangle attached to this test.
[edit]
Language: Beginning Student [custom]; memory limit: 128 MB.
Teachpack: world.rkt.
it's means you added teachpack (*.rkt) has same function name "rectangle".
just clear teachpacks only add you really needed teachpacks
(require 2htdp/image) "image.rkt" have function rectangle.
(require htdp/world) "world.rkt" also have function rectangle.
In same folder create a.rkt b.rkt c.rkt
create file a.rkt
#lang racket
(provide t)
(define (t x) (+ x 1))
create file b.rkt
#lang racket
(provide t)
(define (t x) (+ x 2))
create file c.rkt
#lang racket
(require (file "a.rkt"))
(require (file "b.rkt"))
(t 3)
run c.rkt will shows
module: identifier already required in: t
I have been using Emacs 24.4 for all my math/scientific notes. org-latex-preview is fantastic for this! But recently, I upgraded to a macbook pro with retina display, and I now see that all my equations in org-mode are... fuzzy. Is there a setting I can change to up-res these?
Here is a screenshot:
Thanks!
A couple of years back, I decided to fix this and wrote a patch to add dvisvgm as a render option for latex previews. While this worked great, I never submitted it (no time or knowledge on how to officially patch org).
Today, I was thrilled to discover that org-mode v9.0.6, now has this feature!
To activate, first check that you have dvisvgm on your system. Then update org-mode and add the following line to your init.el file:
(setq org-latex-create-formula-image-program 'dvisvgm)
And presto!
I found a solution that works a little more generally for all inline images. First make sure any generated images are created with a scale factor of 2. For example for LaTeX code blocks and inline LaTeX snippets this works by
(plist-put org-format-latex-options :scale 2)
Then you make org scale all inlined images back.
For LaTeX code blocks we can advise org--create-inline-image like so:
(defun my/image-scale-advice (image)
(let* ((factor (image-property image :scale))
(new-factor (if factor
(/ factor 2.0)
0.5)))
(image--set-property image :scale new-factor)
image))
(advice-add 'org--create-inline-image :filter-return #'my/image-scale-advice)
This divides any already existing scaling-factor by 2 or sets a scaling factor of 0.5 if none is present.
For inline LaTeX snippets we can advise org--make-preview-overlay like so:
(defun my/overlay-scale-advice (beg end image &optional imagetype)
(mapc (lambda (ov) (if (equal (overlay-get ov 'org-overlay-type) 'org-latex-overlay)
(overlay-put ov
'display
(list 'image :type (or (intern imagetype) 'png) :file image :ascent 'center :scale 0.5))))
(overlays-at beg)))
(advice-add 'org--make-preview-overlay :after #'my/overlay-scale-advice)
This should result in much crispier inline images on Retina displays.
By default orgmode latex preview do not support retina, so on mac with retina screen, latex preview will be fuzzy.
However, we can hack org.el to achieve the function. Just follow steps below:
get the proper version of emacs
To instead use the Yamamoto Mitsuharu version of Emacs 25.1 (with more mac-specific features):
brew tap railwaycat/emacsmacport
brew install emacs-mac
and finally link it to your Applications folder:
brew linkapps emacs-mac
this version emacs will support retina image display.
change org-format-latex-options
change scale from 1.0 to 2.0, to generate 2x size image.
delete org.elc
add function to org.el
(defun galaxy-compose-image-filename-2x(image-file-name)
(concat (file-name-directory image-file-name) (file-name-base image-file-name) "#2x." (file-name-extension image-file-name)))
and eval the function.
modify function org-format-latex
change fragment:
(unless (file-exists-p movefile)
(org-create-formula-image value movefile options forbuffer processing-type)
to
(unless (file-exists-p movefile)
(org-create-formula-image value movefile options forbuffer processing-type)
(setq filename-2x (galaxy-compose-image-filename-2x movefile))
(rename-file movefile filename-2x)
(call-process-shell-command "convert" nil nil nil nil (concat "\"" filename-2x "\" -scale \"50%\" -quality \"100%\"" ) (concat "\"" movefile "\"" )))
and eval the function.
Now, you can preview latex with 2x size image for mac retina screen.
I have tried the
emacs-mac-port
If I create 2 files foo.png foo#2x.png on the same dir ,this will work.
I´d like to have the following workflow using emacs 23.4 as a python (2.7) IDE on Debian:
Emacs initiates with 2 windows side-by-side when opening a file like $ emacs file.py
There´s already a shell in the left window and the buffer file.py in the right window.
A shortcut executes the code (and another shortcut for parts of it) and the result can be seen in the left window (ipython shell). The focus remains at the right window and the buffers don´t change when the command is executed.
A shortcut easily switches the focus from left to right and the other way around.
I could, so far, accomplish everything except the second item (I have to make the buffer visible manually), which seems simple. I´ve been reading the emacs lips reference manual, so that I can customize emacs myself, but I´m still a beginner in emacs. I also found some similar questions, but not fully helpful. Here are some relevant parts of my .emacs.
;; Initial frame size and position (1280x1024)
(setq default-frame-alist
'((top . 45) (left . 45)
(width . 142) (height . 54)))
(if (window-system)
(split-window-horizontally (floor (* 0.49 (window-width))))
)
; python-mode
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.3")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
; use IPython
(setq-default py-shell-name "ipython")
(setq-default py-which-bufname "IPython")
; use the wx backend, for both mayavi and matplotlib
(setq py-python-command-args
'("--gui=wx" "--pylab=wx" "--colors=linux"))
(setq py-force-py-shell-name-p t)
; switch to the interpreter after executing code
(setq py-shell-switch-buffers-on-execute-p t)
;(setq py-switch-buffers-on-execute-p t)
; don't split windows
(setq py-split-windows-on-execute-p nil)
; try to automagically figure out indentation
(setq py-smart-indentation t)
(defun goto-python-shell ()
"Go to the python command window (start it if needed)"
(interactive)
(setq current-python-script-buffer (current-buffer))
(py-shell)
(end-of-buffer)
)
(goto-python-shell)
I believe the solution is simple and lies on the functions/variables: switch-to-buffer, initial-buffer-choice, other-window, py-shell-switch-buffers-on-execute-p, py-switch-buffers-on-execute-p.
However, I still couldn't find a solution that makes it all work.
EDIT:
I was able to have my desired behavior, substituting the last part for:
(switch-to-buffer (py-shell))
(end-of-buffer)
(other-window 3)
(switch-to-buffer (current-buffer))
, since I found out with get-buffer-window that the left window appears as 3 and right window as 6.
When py-split-windows-on-execute-p, py-switch-buffers-on-execute-p are set, it should work as expected - no need to hand-write splitting.
Remains the horizontal/vertical question.
For this python-mode.el provides a customization of py-split-windows-on-execute-function in current trunk:
https://code.launchpad.net/python-mode
I'm learning LISP for a class. I have a basic workflow setup in Ubuntu with my LISP file in VIM and an interactive LISP prompt in a terminal that I'm using to test code as I write it. Is there a way to get LISP to load a specific file every time I type a command? It's getting a bit tiring having to constantly input (load 'initial-code.cl) (yes, even when I am using the terminal's history).
Can always try:
(let (fn)
(defun l (&optional filename)
(if filename
(setf fn filename))
(load fn)))
Works like this:
[2]> (l "x.lisp")
;; Loading file x.lisp ...
;; Loaded file x.lisp
T
[3]> (l)
;; Loading file x.lisp ...
;; Loaded file x.lisp
T
[4]>
Pretty simple.
You can also do something like:
(defun go ()
(load "project.lisp")
(yourfunc 'your 'parameters))
Then you just type (go) and it reloads your file and calls your main entry point.
Or even combine them:
(defun gogo (&rest args)
(l) ;; call (l "file.lisp") first to initialize it
(apply #'yourfunc args))
then you can change your parameters easily
(gogo 1 2)
(gogo 2 4)
Ya know, it's lisp. Don't like something, change it.
With more time, you can write a simple wrapper that can build these on the fly. But you get the idea.
Most Lisp programmers would encourage you to use SLIME.
If you like Eclipse, there is also a Lisp plugin.
I know this doesn't really answer your question, but at least you can be aware of some alternatives.
You can try slimv, it is like slime for vim.