Lewis Caroll, author of the Alice in Wonderland stories, was a very gifted writer. The illustrations for the two Alice books are by Sir John Tenniel (1820-1914).
Why "Mad Hatter"? well, history tells us that some hats were made with a special glue that had toxic effects on makers of hats - hatters. This explains why Caroll knew of hatters that have gone mad (or partly insane). It's no urban legend, it's the simple factual truth.